djangodocs.org

705 sections in Django 5.1 Search all versions →

ModelAdmin methods

Reference The Django admin site ModelAdmin objects

ModelAdmin.get_queryset(request) : The get_queryset method on a ModelAdmin returns a QuerySet of all model instances that can be edited by the admin site.

ModelAdmin methods

Reference The Django admin site ModelAdmin objects

ModelAdmin.response_delete(request,obj_display,obj_id)[source] : Determines the HttpResponse for the delete_view() stage. ModelAdmin.get_formset_kwargs(request,obj,inline,prefix)[source] : A hook for customizing the keyword arguments passed to the constructor of a formset.

Manager methods

Reference django.contrib.auth User model

classmodels.UserManager : The User model has a custom manager that has the following helper methods (in addition to the methods provided by BaseUserManager):

Reference django.contrib.auth Authentication backends

The following backends are available in django.contrib.auth.backends: classBaseBackend[source] : A base class that provides default implementations for all required methods. By default, it will reject any user and provide no permissions.

Reference django.contrib.auth Authentication backends

classAllowAllUsersModelBackend[source] : Same as ModelBackend except that it doesn’t reject inactive users because user_can_authenticate() always returns True.

Distance

Reference Geographic Database Functions

… more resource-intensive). In the following example, the distance from the city of Hobart to every other PointField in the AustraliaCity queryset is calculated: NOTE: Because the distance attribute is a > Distance object, you can easily express > the value …

GeoDjango

Reference

… with Raster Fields Spatial Lookups Distance Queries Compatibility Tables GeoDjango Forms API Field arguments Form field classes Form widgets GIS QuerySet API Reference Spatial Lookups Distance Lookups Geographic Database Functions Area AsGeoJSON AsGML AsKML AsSVG AsWKB AsWKT Azimuth BoundingCircle Centroid …

Reference Databases PostgreSQL notesServer-side cursors

Another option is to wrap each QuerySet using server-side cursors in an atomic() block, because it disables autocommit for the duration of the transaction. This way, the server-side cursor will only live for the duration of the transaction.

Extract

Reference Database Functions Date functions

classExtract(expression,lookup_name=None,tzinfo=None,**extra)[source] Extracts a component of a date as a number. Takes an expression representing a DateField, DateTimeField, TimeField, or DurationField and a lookup_name, and returns the part of the date referenced by lookup_name as an IntegerField.

F() expressions

Reference Query Expressions Built-in Expressions

To access the new value saved this way, the object must be reloaded: As well as being used in operations on single instances as above, F() can be used on QuerySets of object instances, with update().

Expression API

Reference Query Expressions Technical Information

Query expressions implement the query expression API, but also expose a number of extra methods and attributes listed below. All query expressions must inherit from Expression() or a relevant subclass.

Arguments

Reference Model field reference Relationship fieldsManyToManyField

ForeignKey.limit_choices_to : Sets a limit to the available choices for this field when this field is rendered using a ModelForm or the admin (by default, all objects in the queryset are available to choose).

Reference

> > If you use [`prefetch_related()`](querysets.md#django.db.models.query.QuerySet.prefetch_related), > the `add()`, `aadd()`, `remove()`, `aremove()`, `clear()`, > `aclear()`, `set()`, and `aset()` methods clear the prefetched > cache.

Topic guide

… deleted: Bulk delete some Publications - references to deleted publications should go: Bulk delete some articles - references to deleted objects should go: After the delete(), the QuerySet cache needs to be cleared, and the referenced objects should be gone:

Topic guide

Article.objects.filter(reporter__first_name="John") <QuerySet [<Article: John's second story>, <Article: This is a test>]> Query twice over the related field.

Topic guide Models Model methods

NOTE: Note that the delete() method for an object is not > necessarily called when deleting objects in bulk using a > QuerySet or as a result of a cascading > delete.

Creating objects

Topic guide Making queries

To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table.

Topic guide Making queries Retrieving objectsLookups that span relationships

When spanning a ManyToManyField or a reverse ForeignKey (such as from Blog to Entry), filtering on multiple attributes raises the question of whether to require each attribute to coincide in the same related object.