chrisdl/Django-QuerySet-Cheatsheet
Tentang
A cheatsheet (or really more of a reference) for when I am solving the worlds problems in the Django shell
Pengembangan web dan aplikasiPertumbuhan bintang terukur
- dalam 24 jam
- 0
- dalam 7 hari
- 0
- dalam 15 hari
- 0
- dalam 1 bulan
- Pembaruan tertunda
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
AsliCheatsheet for Django QuerySets
Current Django Version: 5.2
Mandatory impostor syndrome notice: The documentation has gotten a lot easier to navigate and this cheatsheet makes a lot less sense nowadays, but many people seem to be actively using it, so i'll keep it updated for a few more versions.
Methods that return new QuerySets
Can be chained:
Entry.objects.filter(**kwargs).exclude(**kwargs).order_by(**kwargs)
- filter
- exclude
- annotate
- alias
- order_by
- reverse
- distinct
- values
- values_list
- dates
- datetimes
- none
- all
- union
- intersection
- difference
- select_related
- prefetch_related
- extra
- defer
- only
- using
- select_for_update
- raw
Operators that return new QuerySets
Methods that do not return QuerySets
- get
- create
- get_or_create
- update_or_create
- bulk_create
- bulk_update
- count
- in_bulk
- iterator
- latest
- earliest
- first
- last
- aggregate
- exists
- contains
- update
- delete
- as_manager
- explain
Field lookups (link)
**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
- date
- year
- iso_year
- month
- day
- week
- week_day
- iso_week_day
- quarter
- time
- hour
- minute
- second
- isnull
- regex
- iregex
Protip: Use in to avoid chaining filter() and exclude()
Entry.objects.filter(status__in=['Hung over', 'Sober', 'Drunk'])
Aggregation functions (link)
Query-related tools (link)

Django-QuerySet-Cheatsheet by @chrisdl and @briandant is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
The contributors are as gods among ants and shall forever be remembered as such.
The Django web framework referenced in the Django-QuerySet-Cheatsheet is © 2005-2025 Django Software Foundation. Django is a registered trademark of the Django Software Foundation.
Jelajahi proyek terkait
Memuat…