djangodocs.org

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

ModelChoiceField

Reference Form fields Fields which handle relationships

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

4.2 5.0 5.1 5.2 6.0

ModelChoiceIterator

Reference Form fields Fields which handle relationshipsIterating relationship choices

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.

4.2 5.0 5.1 5.2 6.0

DoesNotExist

Reference Model class reference Attributes

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.

4.2 5.0 5.1 5.2 6.0

When

Reference Conditional Expressions The conditional expression classes

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.

4.2 5.0 5.1 5.2 6.0

Func() expressions

Reference Query Expressions Built-in 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 …

4.2 5.0 5.1 5.2 6.0

Aggregate() expressions

Reference Query Expressions Built-in 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.

4.2 5.0 5.1 5.2 6.0

Reference Query Expressions Built-in ExpressionsSubquery() 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.

4.2 5.0 5.1 5.2 6.0

Reference Query Expressions Built-in ExpressionsSubquery() 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.

4.2 5.0 5.1 5.2 6.0

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.

6.0

pre_delete

Reference Signals Model 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.

4.2 5.0 5.1 5.2 6.0

post_delete

Reference Signals Model 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.

4.2 5.0 5.1 5.2 6.0

Queries & the ORM

Topic guide Asynchronous support Async views

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.

4.2 5.0 5.1 5.2 6.0

Custom managers

Topic guide Managers

There are two reasons you might want to customize a Manager: to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.

4.2 5.0 5.1 5.2 6.0

Profile first

Topic guide Database access optimization

Use QuerySet.explain() to understand how specific QuerySets are executed by your database. You may also want to use an external project like django-debug-toolbar, or a tool that monitors your database directly.

4.2 5.0 5.1 5.2 6.0