Reference
The Django admin site
ModelAdmin objects
ModelAdmin.formfield_for_manytomany(db_field,request,**kwargs) : Like the formfield_for_foreignkey method, the formfield_for_manytomany method can be overridden to change the default formfield for a many to many field.
Reference
django.contrib.auth
Authentication backends
[`with_perm()`](#django.contrib.auth.backends.ModelBackend.with_perm) also allows an object to be passed as a parameter, but unlike others methods it returns an empty queryset if `obj is not None`. `authenticate(request,username=None,password=None,**kwargs)`[[source]](https://github.com/django/django/blob/stable/5.2.x/django/contrib/auth/backends.py#L59) `aauthenticate(request,username=None,password=None,**kwargs)` : *Asynchronous version*: `aauthenticate()` Tries to authenticate `username` with `password` by calling [`User.check_password`](#django.contrib.auth.models.User.check_password).
Reference
Geographic Database Functions
Measurements
classGeometryDistance(expr1,expr2,**extra)[source] Availability: PostGIS Accepts two geographic fields or expressions and returns the distance between them. When used in an order_by() clause, it provides index-assisted nearest-neighbor result sets.
Reference
The sitemap framework
The sitemap framework provides a convenience class for a common case: classGenericSitemap(info_dict,priority=None,changefreq=None,protocol=None)[source] : The django.contrib.sitemaps.GenericSitemap class allows you to create a sitemap by passing it a dictionary which has to contain at least a queryset entry.
Reference
The sitemap framework
Shortcuts
Here’s an example of a URLconf using GenericSitemap:
Reference
Django Exceptions
Django Core Exceptions
exceptionObjectDoesNotExist[source] : The base class for Model.DoesNotExist exceptions. A try/except for ObjectDoesNotExist will catch DoesNotExist exceptions for all models.
Reference
Django Exceptions
Django Core Exceptions
exceptionMultipleObjectsReturned[source] : The base class for Model.MultipleObjectsReturned exceptions. A try/except for MultipleObjectsReturned will catch MultipleObjectsReturned exceptions for all models.
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.
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
Reference
Form fields
Fields which handle relationships
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.
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.
Reference
Form fields
Fields which handle relationshipsIterating relationship choices
classModelChoiceIteratorValue(value,instance)[source] : Two arguments are required:
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.
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.
Reference
Conditional Expressions
Advanced queries
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:
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 …
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.
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.
Reference
Query Expressions
Built-in ExpressionsSubquery() expressions
… returns a boolean value and Exists() may be used as a condition in When expressions, or to directly filter a queryset: This will ensure that the subquery will not be added to the SELECT columns, which may result in a …
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.