Reference
The Django admin site
ModelAdmin objects
The ModelAdmin is very flexible. It has several options for dealing with customizing the interface. All options are defined on the ModelAdmin subclass: ModelAdmin.actions : A list of actions to make available on the change list page.
Reference
The Django admin site
ModelAdmin objects
Call super().delete_model() to delete the object using Model.delete(). ModelAdmin.delete_queryset(request,queryset)[source] : The delete_queryset() method is given the HttpRequest and a QuerySet of objects to be deleted. Override this method to customize the deletion process for the “delete selected objects” action.
Reference
The Django admin site
ModelAdmin objects
ModelAdmin.get_form(request,obj=None,**kwargs)[source] : Returns a ModelForm class for use in the admin add and change views, see add_view() and change_view(). ModelAdmin.get_formsets_with_inlines(request,obj=None)[source] : Yields (FormSet, InlineModelAdmin) pairs for use in admin add and change views.
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
Returns an empty queryset if no users who have the `perm` found. If `is_active` is `True` (default), returns only active users, or if `False`, returns only inactive users. Use `None` to return all users irrespective of active state.
Reference
Geographic Database Functions
Measurements
… 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 …
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 Measurements Relationships Operations Editors Input format Output format Miscellaneous Measurement …
Reference
GeoDjango Tutorial
Spatial Queries
When doing spatial queries, GeoDjango automatically transforms geometries if they’re in a different coordinate system.
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.
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.
Reference
Query Expressions
Built-in Expressions
As well as being used in operations on single instances as above, F() can be used with update() to perform bulk updates on a QuerySet.
Reference
Query Expressions
Technical Information
As an example, an expression implementing `NULLS LAST` would change its value to be `NULLS FIRST`. Modifications are only required for expressions that implement sort order like `OrderBy`. This method is called when [`reverse()`](querysets.md#django.db.models.query.QuerySet.reverse) is called on a queryset.
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
Model Meta options
Available Meta options
Options.ordering : The default ordering for the object, for use when obtaining lists of objects: NOTE: In GROUP BY queries (for example, > those using values() and annotate()), the > default ordering is not applied.
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
To define a many-to-many relationship, use ManyToManyField.
Topic guide
… to deleted publications will no longer be included in the related entries: 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 > (see on_delete).