Questions tagged [django-queryset]

Django querysets are the primary abstraction for retrieving objects from Django's ORM system

Django querysets are the primary abstraction for retrieving objects from Django's ORM system using custom SQL queries. They abstract both the creation of queries as well as the assembly of the model objects that are returned.

See also

6784 questions
957
votes
17 answers

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 a; int x; I want to do results =…
MikeN
  • 45,039
  • 49
  • 151
  • 227
822
votes
16 answers

How to combine multiple QuerySets in Django?

I'm trying to build the search for a Django site I am building, and in that search, I am searching across three different models. And to get pagination on the search result list, I would like to use a generic object_list view to display the results.…
espenhogbakk
  • 11,508
  • 9
  • 39
  • 38
608
votes
9 answers

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!="") So, what is the equivalent to the above?
Val Neekman
  • 17,692
  • 14
  • 63
  • 66
477
votes
8 answers

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 = False How would I do this in Django?…
Mez
  • 24,430
  • 14
  • 71
  • 93
473
votes
10 answers

How to delete a record in Django models?

I want to delete a particular record like: delete from table_name where id = 1; How can I do this in a django model?
user426795
  • 11,073
  • 11
  • 35
  • 35
450
votes
4 answers

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'm looking to get them all in one query with…
ajwood
  • 18,227
  • 15
  • 61
  • 104
426
votes
6 answers

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(income__gte=5000, income=0) This doesn't work, because it ANDs…
Elisa
  • 6,865
  • 11
  • 41
  • 56
408
votes
5 answers

Getting the SQL from a Django QuerySet

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.
exupero
  • 9,136
  • 8
  • 47
  • 63
387
votes
24 answers

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 None instead?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
371
votes
8 answers

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 have a date between 1-Jan-2011 and 31-Jan-2011?
user469652
  • 48,855
  • 59
  • 128
  • 165
298
votes
5 answers

Django values_list vs values

In Django, what's the difference between the following two: Article.objects.values_list('comment_id', flat=True).distinct() versus: Article.objects.values('comment_id').distinct() My goal is to get a list of unique comment ids under each Article.…
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
280
votes
7 answers

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 without querying again. # Else, do something…
Niklas
  • 5,736
  • 7
  • 35
  • 42
254
votes
2 answers

">", "<", ">=" and "<=" don't work with "filter()" in Django

With = below, I could filter persons by age: qs = Person.objects.filter(age = 20) # ↑ Here But with >, <, >= and <= below, I couldn't filter persons by age: qs = Person.objects.filter(age > 20) …
Finglish
  • 9,692
  • 14
  • 70
  • 114
221
votes
20 answers

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 not considering time, but I want "any time". Is…
Xidobix
  • 2,538
  • 3
  • 17
  • 10
218
votes
8 answers

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 something like: update my_table set field_1 = 'some…
John
  • 21,047
  • 43
  • 114
  • 155
1
2 3
99 100