Topic guide Making queries
Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.
743 sections across all versions Narrow to Django 6.0 (current) →
Topic guide Making queries
Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.
Reference Form fields Fields which handle relationships
classModelMultipleChoiceField(**kwargs)[source] : - Default widget: SelectMultiple - Empty value: An empty QuerySet (self.queryset.none()) - Normalizes to: A QuerySet of model instances. - Validates that every id in the given list of values exists in the queryset.
Topic guide Django shortcut functions
get_object_or_404(klass,*args,**kwargs)[source] aget_object_or_404(klass,*args,**kwargs) : Asynchronous version: aget_object_or_404()
… - quarter - field lookup type - query_pk_and_slug (django.views.generic.detail.SingleObjectMixin attribute) - query_string (django.views.generic.base.RedirectView attribute) - QueryDict (class in django.http) - queryset | - QuerySet (class in django.db.models.query) - queryset (django.views.generic.detail.SingleObjectMixin attribute) - (django.views.generic.list.MultipleObjectMixin attribute) - (ModelChoiceField attribute) - (ModelMultipleChoiceField attribute) …
Internals Django Deprecation Timeline
Calling QuerySet.values_list() with flat=True and no field name will raise TypeError. The use of None when querying a JSONField top-level value will compile to SQL IS NULL. Use JSONNull to query for a JSON null value instead.
Reference Settings Core SettingsDATABASES
Default: False Set this to True if you want to disable the use of server-side cursors with QuerySet.iterator(). Transaction pooling and server-side cursors describes the use case. This is a PostgreSQL-specific setting.
Topic guide
Article.objects.filter(publications__title__startswith="Science").distinct() <QuerySet [<Article: NASA uses Python>]> Reverse m2m queries are supported (i.e., starting at the table that doesn’t have a ManyToManyField): Excluding a related item works as you would expect, too (although the SQL involved is a little complex): If …
Topic guide Making queries
To retrieve objects from your database, construct a QuerySet via a Manager on your model class. A QuerySet represents a collection of objects from your database. It can have zero, one or many filters.
Topic guide Making queries
Sometimes you want to set a field to a particular value for all the objects in a QuerySet. You can do this with the update() method. For example: You can only set non-relation fields and ForeignKey fields using this method.
Reference The Django admin site ModelAdmin objects
ModelAdmin.search_help_text : Set search_help_text to specify a descriptive text for the search box which will be displayed below it. ModelAdmin.show_full_result_count : Set show_full_result_count to control whether the full count of objects should be displayed on a filtered admin page (e.g.
Topic guide Django shortcut functions
get_list_or_404(klass,*args,**kwargs)[source] aget_list_or_404(klass,*args,**kwargs) : Asynchronous version: aget_list_or_404()
How-to
Django offers a wide variety of built-in lookups for filtering (for example, exact and icontains). This documentation explains how to write custom lookups and how to alter the working of existing lookups.
Reference Model field reference Relationship fieldsManyToManyField
ForeignKey.related_name : The name to use for the relation from the related object back to this one. It’s also the default value for related_query_name (the name to use for the reverse filter name from the target model).
… | | --- | --- | | - A (class in django.contrib.gis.measure) - aadd() (RelatedManager method) - aaggregate() (in module django.db.models.query.QuerySet) - aauthenticate() (in module django.contrib.auth) - (ModelBackend method) - (RemoteUserBackend method) - Abs (class in django.db.models.functions) - ABSOLUTE_URL_OVERRIDES - …
| | | | --- | --- | | - each_context() (AdminSite method) - earliest() (in module django.db.models.query.QuerySet) - editable (Field attribute) - ELLIPSIS (Paginator attribute) - ellipsoid (SpatialReference attribute) - email (models.User attribute) - EMAIL_BACKEND - setting - EMAIL_FIELD …
… - import_wkt() (SpatialReference method) - import_xml() (SpatialReference method) - ImproperlyConfigured - in - field lookup type - in_bulk() (in module django.db.models.query.QuerySet) - include - template tag - include (ExclusionConstraint attribute) - (Index attribute) - (UniqueConstraint attribute) - include() (in module …
Internals Django Deprecation Timeline
Calling QuerySet.aiterator() after prefetch_related() without providing a chunk_size will raise ValueError. The Extract and Trunc database functions will capture the current timezone in migrations when the tzinfo argument is omitted and USE_TZ is True.
Tutorial Writing your first Django app, part 3 Raising a 404 error
It’s a very common idiom to use get() and raise Http404 if the object doesn’t exist. Django provides a shortcut.
Reference Generic display views
classdjango.views.generic.list.ListView : A page representing a list of objects. classdjango.views.generic.list.BaseListView : A base view for displaying a list of objects.
Reference Date-based mixins
classDateMixin[source] : A mixin class providing common behavior for all date-based views.