Similar to that of filter, exclude()
function in models can be used to eliminate the rows that meet a specific criteria.
For example, to exclude the record whose first_name
is James
, a query can be written as follows :
Person.objects.exclude(first_name='James')
The SQL query
for the above code will be as following :
SELECT * FROM Person WHERE first_name != 'James';
Similarly, multiple conditions can be given as well in the exclude()
function.
For example, to eliminate the records whose first_name
is James
and last_name
is Forbes
:
Person.objects.exclude(first_name='James', last_name='Forbes')
The SQL query
for the above code will be as following :
SELECT * FROM Person WHERE first_name != 'James' AND last_name != 'Forbes';