Questions tagged [django-queryset]
Django querysets are the primary abstraction for retrieving objects from Django's ORM system
6,083
questions
843votes
17answers
536kviews
How do I do a not equal in Django queryset filtering?
In Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne or != (not equals)? I want to filter out using a not equals. For example, for
Model:
bool ...
765votes
14answers
375kviews
How can I combine two or more querysets in a Django view?
I am trying to build the search for a Django site I am building, and in that search, I am searching in three different models. And to get pagination on the search result list, I would like to use a ...
544votes
9answers
419kviews
How to filter empty or NULL names in a QuerySet?
I have first_name, last_name & alias (optional) which I need to search for. So, I need a query to give me all the names that have an alias set.
Only if I could do:
Name.objects.filter(alias!="")
...
424votes
8answers
219kviews
How do I do an OR filter in a Django query?
I want to be able to list the items that either a user has added (they are listed as the creator) or the item has been approved.
So I basically need to select:
item.creator = owner or item.moderated ...
395votes
3answers
287kviews
How can I filter a Django query with a list of values?
I'm sure this is a trivial operation, but I can't figure out how it's done.
There's got to be something smarter than this:
ids = [1, 3, 6, 7, 9]
for id in ids:
MyModel.objects.filter(pk=id)
I'...
388votes
4answers
223kviews
How to perform OR condition in django queryset?
I want to write a Django query equivalent to this SQL query:
SELECT * from user where income >= 5000 or income is NULL.
How to construct the Django queryset filter?
User.objects.filter(...
367votes
5answers
210kviews
Getting the SQL from a Django QuerySet [duplicate]
How do I get the SQL that Django will use on the database from a QuerySet object? I'm trying to debug some strange behavior, but I'm not sure what queries are going to the database.
325votes
20answers
277kviews
How do I get the object if it exists, or None if it does not exist in Django?
When I ask the model manager to get an object, it raises DoesNotExist when there is no matching object.
go = Content.objects.get(name="baby")
Instead of DoesNotExist, how can I have go be ...
324votes
8answers
387kviews
How do I filter query objects by date range in Django?
I've got a field in one model like:
class Sample(models.Model):
date = fields.DateField(auto_now=False)
Now, I need to filter the objects by a date range.
How do I filter all the objects that ...
244votes
7answers
208kviews
Checking for empty queryset in Django
What is the recommended idiom for checking whether a query returned any results?
Example:
orgs = Organisation.objects.filter(name__iexact = 'Fjuk inc')
# If any results
# Do this with the results ...
234votes
4answers
234kviews
Django values_list vs values
In Django, what's the difference between the following two:
Article.objects.values_list('comment_id', flat=True).distinct()
vs
Article.objects.values('comment_id').distinct()
My goal is to get a ...
229votes
1answer
207kviews
How to do a less than or equal to filter in Django queryset?
I am attempting to filter users by a custom field in each users profile called profile. This field is called level and is an integer between 0-3.
If I filter using equals, I get a list of users with ...
204votes
20answers
233kviews
How can I filter a date of a DateTimeField in Django?
I am trying to filter a DateTimeField comparing with a date. I mean:
MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22))
I get an empty queryset list as an answer because (I think) I am ...
185votes
6answers
259kviews
How to select a record and update it, with a single queryset in Django?
How do I run an update and select statements on the same queryset rather than having to do two queries:
- one to select the object
- and one to update the object
The equivalent in SQL would be ...
160votes
3answers
46kviews
Difference between Django's annotate and aggregate methods?
Django's QuerySet has two methods, annotate and aggregate. The documentation says that:
Unlike aggregate(), annotate() is not a terminal clause. The output of the annotate() clause is a QuerySet.
...