'How to check the filter object is returning any data or not in Django

I have a model named Rooms. Now I want to check if a specific row exists in the model or not by this line of code:

checkroom = Rooms.objects.filter(building_id=building_id, room_no=room_no).first()

If the row doesn't exist, then I want to print some text: How Can I check the condition?

I have used

if checkroom:

this condition to check if it exists. but now I want to check if it doesn't exist separately with an if condition.



Solution 1:[1]

I think you can use this :

if not checkroom:
    # Do this...
else:
    # Do that...

Solution 2:[2]

You have to use exists query which is faster then retrieve a row.

is_exists_room = Rooms.objects.filter(building_id=building_id, room_no=room_no).exists()

if not is_exists_room:
    print("The room doesn't exist!")

Solution 3:[3]

In order to use, axios you have to import the axios. Considering you have already installed axios already in your project as it is third party library.

import axios from 'axios';

Add above line in the component, wherever you use the package.

Solution 4:[4]

First install both axios and vue-axios packages.

npm install axios vue-axios

Then in app.js file write this code:

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

// this is the default base url in laravel
axios.defaults.baseURL = 'http://127.0.0.1:8000';

// this line is written to avoid csrf error
window.axios.defaults.headers.common = {
    'X-Requested-With': 'XMLHttpRequest',
    'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')
};

Then when you want to use axios just write this.axios.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mostafa Rezaie
Solution 2 Mukhtor Rasulov
Solution 3 Varit J Patel
Solution 4 Emad