Reputation: 8471
I'm just curious. What is the correct way or should I say, the efficient way in filtering data? I'm still learning. Say you have thousands of records then you want to retrieve all active records.
Should it be done like this.
Records.objects.all().exclude(active=False)
Or
Records.objects.filter(active=True)
Do they have a difference or just the same?
Upvotes: 1
Views: 1281
Reputation: 473813
These two expressions produce different queries.
You always can inspect them by looking at the query
attribute of a queryset:
>>> print Records.objects.all().exclude(active=False).query
Records.objects.all().exclude(active=False)
produces:
SELECT
<list_of_fields>
FROM
`<table_name>`
WHERE
NOT (`<table_name>`.`active` = False)
Records.objects.filter(active=True)
produces:
SELECT
<list_of_fields>
FROM
`<table_name>`
WHERE
`<table_name>`.`active` = True
See also:
Upvotes: 1