djangodocs.org

680 sections in Django 5.0 Search all versions →

Reference Admin actions Advanced action techniques

Actions may limit their availability to users with specific permissions by wrapping the action function with the action() decorator and passing the permissions argument: The make_published() action will only be available to users that pass the ModelAdmin.has_change_permission() check.

The action decorator

Reference Admin actions

action(*,permissions=None,description=None) : This decorator can be used for setting specific attributes on custom action functions that can be used with actions:

Using a SimpleListFilter

Reference ModelAdmin List Filters

… own list filter by subclassing django.contrib.admin.SimpleListFilter. You need to provide the title and parameter_name attributes, and override the lookups and queryset methods, e.g.: NOTE: As a convenience, the HttpRequest object is passed to the lookups > and queryset methods, for …

ModelAdmin methods

Reference The Django admin site ModelAdmin objects

ModelAdmin.save_related(request,form,formsets,change) : The save_related method is given the HttpRequest, the parent ModelForm instance, the list of inline formsets and a boolean value based on whether the parent is being added or changed.

GeometryDistance

Reference Geographic Database Functions

classGeometryDistance(expr1,expr2,**extra) 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.

Shortcuts

Reference The sitemap framework

The sitemap framework provides a convenience class for a common case: classGenericSitemap(info_dict,priority=None,changefreq=None,protocol=None) : 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.

ObjectDoesNotExist

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.

MultipleObjectsReturned

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.

ModelChoiceField

Reference Form fields Fields which handle relationships

classModelChoiceField(**kwargs) : - 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

ModelChoiceIterator

Reference Form fields Fields which handle relationshipsIterating relationship choices

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

ModelChoiceIteratorValue

Reference Form fields Fields which handle relationshipsIterating relationship choices

classModelChoiceIteratorValue(value,instance) : Two arguments are required:

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.

When

Reference Conditional Expressions The conditional expression classes

classWhen(condition=None,then=None,**lookups) 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.

Conditional update

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:

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 …

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.

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.