djangodocs.org

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

Field lookups

Topic guide Making queries Retrieving objects

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(). Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore).

4.2 5.0 5.1 5.2 6.0

The pk lookup shortcut

Topic guide Making queries Retrieving objects

… equivalent: The use of pk isn’t limited to __exact queries – any query term can be combined with pk to perform a query on the primary key of a model: pk lookups also work across joins. For example, these three …

4.2 5.0 5.1 5.2 6.0

Topic guide Making queries Querying JSONField

… 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 in an …

6.0

Topic guide Formsets

The absolute_max parameter to formset_factory() allows limiting the number of forms that can be instantiated when supplying POST data. This protects against memory exhaustion attacks using forged POST requests: When absolute_max is None, it defaults to max_num + 1000.

4.2 5.0 5.1 5.2 6.0

can_order

Topic guide Formsets Dealing with ordering and deletion of forms

BaseFormSet.can_order Default: False Lets you create a formset with the ability to order: This adds an additional field to each form. This new field is named ORDER and is an forms.IntegerField.

4.2 5.0 5.1 5.2 6.0

can_delete

Topic guide Formsets Dealing with ordering and deletion of forms

BaseFormSet.can_delete Default: False Lets you create a formset with the ability to select forms for deletion: Similar to can_order this adds a new field to each form named DELETE and is a forms.BooleanField.

4.2 5.0 5.1 5.2 6.0

A full example

Topic guide Creating forms from models ModelForm

Consider this set of models: With these models, the ModelForm subclasses above would be roughly equivalent to this (the only difference being the save() method, which we’ll discuss in a moment.):

4.2 5.0 5.1 5.2 6.0

Model formsets

Topic guide Creating forms from models

classmodels.BaseModelFormSet Like regular formsets, Django provides a couple of enhanced formset classes to make working with Django models more convenient. Let’s reuse the Author model from above: Using fields restricts the formset to use only the given fields.

4.2 5.0 5.1 5.2 6.0

Setting test cookies

Topic guide How to use sessions

As a convenience, Django provides a way to test whether the user’s browser accepts cookies. Call the set_test_cookie() method of request.session in a view, and call test_cookie_worked() in a subsequent view – not in the same view call.

4.2 5.0 5.1 5.2 6.0

Topic guide Format localization

When formatting is enabled, Django can use localized formats when parsing dates, times and numbers in forms. That means it tries different formats for different locales when guessing the format used by the user when inputting data on forms.

4.2 5.0 5.1 5.2 6.0