djangodocs.org

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

ObjectDoesNotExist

Reference Django exceptions

exceptionObjectDoesNotExist[source] : The base class for Model.DoesNotExist exceptions. A try/except for ObjectDoesNotExist will catch DoesNotExist exceptions for all models.

dev

MultipleObjectsReturned

Reference Django exceptions

exceptionMultipleObjectsReturned[source] : The base class for Model.MultipleObjectsReturned exceptions. A try/except for MultipleObjectsReturned will catch MultipleObjectsReturned exceptions for all models.

dev

Reference Form fields

Two fields are available for representing relationships between models: ModelChoiceField and ModelMultipleChoiceField. Both of these fields require a single queryset parameter that is used to create the choices for the field.

dev

ModelChoiceField

Reference Form fields

classModelChoiceField(**kwargs)[source] : - Default widget: Select - Empty value: None - Normalizes to: A model instance. - Validates that the given id exists in the queryset. - Error message keys: required, invalid_choice

dev

ModelMultipleChoiceField

Reference Form fields

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.

dev

ModelChoiceIterator

Reference Form fields

classModelChoiceIterator(field)[source] : The default class assigned to the iterator attribute of ModelChoiceField and ModelMultipleChoiceField. An iterable that yields 2-tuple choices from the queryset.

dev

DoesNotExist

Reference Model class reference

exceptionModel.DoesNotExist : This exception is raised by the ORM when an expected object is not found. For example, QuerySet.get() will raise it when no object is found for the given lookups.

dev

When

Reference Conditional Expressions

classWhen(condition=None,then=None,**lookups)[source] A When() object is used to encapsulate a condition and its result for use in the conditional expression. Using a When() object is similar to using the filter() method.

dev

Conditional update

Reference Conditional Expressions

Let’s say we want to change the account_type for our clients to match their registration dates. We can do this using a conditional expression and the update() method:

dev

Func() expressions

Reference Query Expressions

… directly: or they can be used to build a library of database functions: But both cases will result in a queryset where each model is annotated with an extra attribute field_lower produced, roughly, from the following SQL: See Database Functions …

dev

Aggregate() expressions

Reference Query Expressions

This is useful for specifying a value to be returned other than None when the queryset (or grouping) contains no entries. The **extra kwargs are key=value pairs that can be interpolated into the template attribute.

dev

Reference Query Expressions

To prevent a subquery from returning multiple rows, a slice ([:1]) of the queryset is used: In this case, the subquery must only return a single column and a single row: the email address of the most recently created comment.

dev

Reference Query Expressions

values('post') aggregates comments by Post. Finally, annotate(...) performs the aggregation. The order in which these queryset methods are applied is important. In this case, since the subquery must be limited to a single column, values('total') is required.

dev

Reference Model instance reference

If you delete a field from a model instance, accessing it again reloads the value from the database: Model.refresh_from_db(using=None,fields=None,from_queryset=None)[source] Model.arefresh_from_db(using=None,fields=None,from_queryset=None) Asynchronous version: arefresh_from_db() If you need to reload a model’s values from the database, you can use the refresh_from_db() method.

dev

pre_delete

Reference Signals

django.db.models.signals.pre_delete Sent at the beginning of a model’s delete() method and a queryset’s delete() method. Arguments sent with this signal: sender The model class. instance The actual instance being deleted. using The database alias being used.

dev

post_delete

Reference Signals

django.db.models.signals.post_delete Like pre_delete, but sent at the end of a model’s delete() method and a queryset’s delete() method. Arguments sent with this signal: sender : The model class. instance : The actual instance being deleted.

dev

Queries & the ORM

Topic guide Asynchronous support

With some exceptions, Django can run ORM queries asynchronously: Detailed notes can be found in Asynchronous queries, but in short: All QuerySet methods that cause an SQL query to occur have an a-prefixed asynchronous variant.

dev