Current Django Version: 1.7
Methods that return new QuerySets
Can be chained:
Entry.objects.filter(**kwargs).exclude(**kwargs).order_by(**kwargs)
- filter
- exclude
- annotate
- order_by
- reverse
- distinct
- values
- values_list
- dates
- datetimes
- none
- all
- select_related
- prefetch_related
- extra
- defer
- only
- using
- select_for_update
- raw (1.7)
- get
- create
- get_or_create
- update_or_create (1.7)
- bulk_create
- count
- in_bulk
- iterator
- latest
- earliest
- first
- last
- aggregate
- exists
- update
- delete
- as_manager (1.7)
Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get().
Example: Entry.objects.get(id__exact=14) # note double underscore.
- exact
- iexact
- contains
- icontains
- in
- gt
- gte
- lt
- lte
- startswith
- istartswith
- endswith
- iendswith
- range
- year
- month
- day
- week_day
- hour
- minute
- second
- isnull
- search
- regex
- iregex
Protip: Use in to avoid chaining filter() and exclude()
Entry.objects.filter(status__in=['Hung over', 'Sober', 'Drunk'])
Django-QuerySet-Cheatsheet by @chrisdl and @briandant is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
The Django web framework referenced in the Django-QuerySet-Cheatsheet is © 2005-2014 Django Software Foundation. Django is a registered trademark of the Django Software Foundation.