djangodocs.org

1413 sections across all versions Narrow to Django 6.0 (current) →

Reference Databases

Another option is to wrap each QuerySet using server-side cursors in an atomic() block, because it disables autocommit for the duration of the transaction. This way, the server-side cursor will only live for the duration of the transaction.

dev

Extract

Reference Database Functions

classExtract(expression,lookup_name=None,tzinfo=None,**extra)[source] Extracts a component of a date as a number. Takes an expression representing a DateField, DateTimeField, TimeField, or DurationField and a lookup_name, and returns the part of the date referenced by lookup_name as an IntegerField.

dev

F() expressions

Reference Query Expressions

As well as being used in operations on single instances as above, F() can be used with update() to perform bulk updates on a QuerySet.

dev

ordering

Reference Model Meta options Available Meta options

Options.ordering : The default ordering for the object, for use when obtaining lists of objects: NOTE: In GROUP BY queries (for example, > those using values() and annotate()), the > default ordering is not applied.

5.2 6.0 dev

Topic guide

Article.objects.filter(reporter__first_name="John") <QuerySet [<Article: John's second story>, <Article: This is a test>]> Query twice over the related field.

dev

Topic guide Models

NOTE: Note that the delete() method for an object is not > necessarily called when deleting objects in bulk using a > QuerySet or as a result of a cascading delete > (see on_delete).

dev

Creating objects

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.

dev

Forward

Topic guide Making queries

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:

dev

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 …

dev

Assertions

Topic guide Testing tools

SimpleTestCase.assertJSONNotEqual(raw,expected_data,msg=None)[source] : Asserts that the JSON fragments raw and expected_data are not equal. See assertJSONEqual() for further details. TransactionTestCase.assertQuerySetEqual(qs,values,transform=None,ordered=True,msg=None)[source] : Asserts that a queryset qs matches a particular iterable of values values.

dev

Glossary

queryset : An object representing some set of rows to be fetched from the database. slug : A short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs.

4.2 5.0 5.1 5.2 6.0

Custom database types

How-to How to create custom model fields Writing a field subclassUseful methods

The methods are also called when constructing a WHERE clause that includes the model field – that is, when you retrieve data using QuerySet methods like get(), filter(), and exclude() and have the model field as an argument.

4.2 5.0 5.1 5.2 6.0

1.7

Internals Django Deprecation Timeline

See the Django 1.5 release notes for more details on these changes. The module django.utils.simplejson will be removed. The standard library provides json which should be used instead. The function django.utils.itercompat.product will be removed.

6.0

Internals Django’s security policies Reporting security issuesReporting guidelines

Reports based on a failure to sanitize user input are not valid security vulnerabilities. It is the developer’s responsibility to properly handle user input. This principle is explained in our security documentation.

5.1 5.2 6.0

Troubleshooting

Reference Applications Initialization process

For example, a common mistake is making a database query to populate form field choices: In the example above, the query from Country.objects.all() is executed during module import, because the QuerySet is iterated over.

5.0 5.1 5.2 6.0

ArchiveIndexView

Reference Class-based generic views - flattened index Date-based views

… accessor): allow_empty [get_allow_empty()] allow_future [get_allow_future()] content_type context_object_name [get_context_object_name()] date_field [get_date_field()] extra_context http_method_names model ordering [get_ordering()] paginate_by [get_paginate_by()] paginate_orphans [get_paginate_orphans()] paginator_class queryset [get_queryset()] response_class [render_to_response()] template_engine template_name [get_template_names()] template_name_suffix Methods as_view() dispatch() get() get_context_data() get_date_list() get_dated_items() get_dated_queryset() get_paginator() head() http_method_not_allowed() paginate_queryset() …

4.2 5.0 5.1 5.2 6.0

DateDetailView

Reference Class-based generic views - flattened index Date-based views

… (with optional accessor): allow_future [get_allow_future()] content_type context_object_name [get_context_object_name()] date_field [get_date_field()] day [get_day()] day_format [get_day_format()] extra_context http_method_names model month [get_month()] month_format [get_month_format()] pk_url_kwarg query_pk_and_slug queryset [get_queryset()] response_class [render_to_response()] slug_field [get_slug_field()] slug_url_kwarg template_engine template_name [get_template_names()] template_name_field template_name_suffix year [get_year()] year_format [get_year_format()] Methods

4.2 5.0 5.1 5.2 6.0