exclude()
Calling filter()
again on a queryset clones it and adds new parameters with an AND operator. Note that multiple keyword parameters can be specified in a single function call.
Conversely, exclude()
clones a queryset and adds any new parameters with an AND NOT operator:
>>> queryset = Employee.objects.filter(first_name="Roger", age=27)
>>> queryset2 = queryset.filter(last_name="Jolly")
>>> queryset3 = queryset.exclude(last_name="Jolly")
>>> any(employee.first_name != "Roger" for employee in queryset2)False
>>> any(employee.last_name != "Jolly" for employee in queryset2)False
>>> all(employee.last_name != "Jolly" for employee in queryset3)True
To retrieve all objects in the table with no parameters, use the all()
function instead of filter()
. all()
can also be used to create a clone of an existing queryset (more on this later).
Iterating through querysets exposes the query results as instances of the associated Model.
...