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
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.
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.
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
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
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 with update() to perform bulk updates on a QuerySet.
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.
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.