Topic guide Making queries
To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table.
728 sections in Django 5.2 Search all versions →
Topic guide Making queries
To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table.
Topic guide Making queries Retrieving objectsLookups that span relationships
When spanning a ManyToManyField or a reverse ForeignKey (such as from Blog to Entry), filtering on multiple attributes raises the question of whether to require each attribute to coincide in the same related object.
Topic guide Making queries Related objectsOne-to-many relationships
Subsequent accesses to the foreign key on the same object instance are cached. Example: Note that the select_related() QuerySet method recursively prepopulates the cache of all one-to-many relationships ahead of time. Example:
Topic guide Pagination
… you methods for accessing the items for each page: NOTE: Note that you can give Paginator a list/tuple, a Django QuerySet, > or any other object with a count() or __len__() method. When > determining the number of objects contained …
| | | | --- | --- | | - handle() (BaseCommand method) - handle_app_config() (AppCommand method) - handle_label() (LabelCommand method) - handle_no_permission() (AccessMixin method) - handle_raw_input() (FileUploadHandler method) - handler400 (in module django.conf.urls) - handler403 (in module django.conf.urls) - …
| | | | --- | --- | | - m (LineString attribute) - (Point attribute) - mail_admins() (in module django.core.mail) - mail_managers() (in module django.core.mail) - make_aware() (in module django.utils.timezone) - make_list - template filter - make_naive() (in module …
How-to How to write custom lookups
You will need to ensure that this registration happens before you try to create any querysets using it. You could place the implementation in a models.py file, or register the lookup in the ready() method of an AppConfig.
Reference The Django admin site ModelAdmin objects
ModelAdmin.save_as : Set save_as to enable a “save as new” feature on admin change forms. ModelAdmin.save_as_continue : When save_as=True, the default redirect after saving the new object is to the change view for that object.
Reference The syndication feed framework The high-level framework
This simple example, taken from a hypothetical police beat news site describes a feed of the latest five news items: To connect a URL to this feed, put an instance of the Feed object in your URLconf.
Reference Model instance reference
In addition to save(), delete(), a model object might have some of the following methods: Model.get_FOO_display() For every field that has choices set, the object will have a get_FOO_display() method, where FOO is the name of the field.
Topic guide Making queries Retrieving objects
Django offers a powerful and intuitive way to “follow” relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes.
Topic guide Making queries Querying JSONField
As with other fields, storing None as the field’s value will store it as SQL NULL. While not recommended, it is possible to store JSON scalar null instead of SQL NULL by using Value(None, JSONField()).
Topic guide Making queries Querying JSONField
To query based on a given dictionary key, use that key as the lookup name: Multiple keys can be chained together to form a path lookup: If the key is an integer, it will be interpreted as an index transform …
… Model index reference Constraints reference Model _meta API Related objects reference Model class reference Model Meta options Model instance reference QuerySet API reference Lookup API reference Query Expressions Conditional Expressions Database Functions Paginator Paginator class Page class Exceptions Request and …
… - buffer_with_style() (GEOSGeometry method) - build_absolute_uri() (HttpRequest method) - build_suite() (DiscoverRunner method) - built-in function - django.conf.settings.configure() - django.core.cache.utils.make_template_fragment_key() - django.core.management.call_command() - django.core.serializers.get_serializer() - django.views.decorators.cache.cache_page() - bulk_create() (in module django.db.models.query.QuerySet) - bulk_update() (in module django.db.models.query.QuerySet) - byteorder (WKBWriter attribute) |
… attribute) - contained - field lookup type - contains - field lookup type - contains() (GEOSGeometry method) - (in module django.db.models.query.QuerySet) - (OGRGeometry method) - (PreparedGeometry method) - contains_aggregate (Expression attribute) - contains_over_clause (Expression attribute) - contains_properly - field lookup …
| | | | --- | --- | | - TabularInline (class in django.contrib.admin) - Tan (class in django.db.models.functions) - teardown_databases() (DiscoverRunner method) - (in module django.test.utils) - teardown_test_environment() (DiscoverRunner method) - (in module django.test.utils) - TelInput (class in django.forms) …
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
NOTE: Due to the way the JSON_EXTRACT and JSON_TYPE SQL functions are > implemented on SQLite, and lack of the BOOLEAN data type, > values() will return True, False, and None instead of > "true", "false", and "null" strings for …
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
values_list(*fields,flat=False,named=False) This is similar to values() except that instead of returning dictionaries, it returns tuples when iterated over.
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
Example: Always use params instead of embedding values directly into where because params will ensure values are quoted correctly according to your particular backend. For example, quotes will be escaped correctly.