How to do a not equal in Django queryset filtering

0 votes

Hii Team,

In Django model QuerySets, I see that there is a __gt and __lt for comparitive values, but is there a __ne/!=/<> (not equals?)

I want to filter out using a not equals:

Example:

Model:
    bool a;
    int x;

I want

results = Model.objects.exclude(a=true, x!=5)

The != is not correct syntax. I tried __ne, <>.

I ended up using:

results = Model.objects.exclude(a=true, x__lt=5).exclude(a=true, x__gt=5)

Any help?

Jun 26, 2020 in Python by kartik
• 37,520 points
28,314 views

1 answer to this question.

0 votes

Hello @kartik,

Your query appears to have a double negative, you want to exclude all rows where x is not 5, so in other words you want to include all rows where x IS 5. I believe this will do the trick.

results = Model.objects.filter(x=5).exclude(a=true)

To answer your specific question, there is no "not equal to" but that's probably because django has both "filter" and "exclude" methods available so you can always just switch the logic round to get the desired result.

Hope it helps!!

Thank You!!

answered Jun 26, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How to check if an element is present in a Django queryset?

Hello @kartik, You can use the following code: if ...READ MORE

answered May 27, 2020 in Python by Niroj
• 82,880 points
14,847 views
0 votes
1 answer

How to do math in a Django template?

Hello @kartik, You can use the add filter: {{ object.article.rating_score|add:"-100" }} Thank ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
6,460 views
0 votes
2 answers

How to use in python for loop not equal marks? example: a!=0

Hello @Azizjon, You can go through the example ...READ MORE

answered Oct 12, 2020 in Python by Gitika
• 65,890 points
2,228 views
0 votes
1 answer

How to write "not equal" in python?

The keywords is and is not are ...READ MORE

answered Sep 18, 2018 in Python by SayantiniDeb
• 200 points
2,384 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,391 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,525 views
0 votes
1 answer

How do I do a not equal in Django queryset filtering?

Hello @kartik, Try this code below: >>> from myapp.models ...READ MORE

answered Jul 3, 2020 in Python by Niroj
• 82,880 points
4,636 views
0 votes
1 answer

How can I do not equal in Django queryset filtering?

Hello @kartik, You can use Q objects for this. They ...READ MORE

answered Nov 17, 2020 in Python by Niroj
• 82,880 points
762 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP