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

1 Answer

0 votes
by

You can use Q objects for this. They can be negated with the ~ operator and combined much like normal Python expressions:

from myapp.models import Entry
from django.db.models import Q

Entry.objects.filter(~Q(id=3))

will return all entries except the one(s) with 3 as their ID:

[<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, ...]

Related questions

0 votes
    In the Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne/!= ... (a=true, x__gt=5) Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How to obtain the SQL query from the queryset in Django?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Suppose I have a model Order, which has a column num -- an order number. Now I want to filter ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I'm running Django 1.0 and I'm close to deploying my app. As such, I'll be changing the DEBUG ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I am currently following along with a somewhat questionable Django tutorial called A complete blog engine using ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I am currently following along with a somewhat questionable Django tutorial called A complete blog engine using ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I am currently following along with a somewhat questionable Django tutorial called A complete blog engine using ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    *Updated with more relevant code. *Updated again: removing chart groupings results in this error: "Unable to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    which of the following statement is not true for filtering the data in ms Excel Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    which of the following statement is not true for filtering the data in ms Excel Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    which of the following statement is not true for filtering the data in ms Excel Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
...