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
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) 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 on QuerySets of object instances, with update().
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
… 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.
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.
Topic guide
Making queries
Related objectsOne-to-many relationships
Subsequent accesses to the foreign key on the same object instance are cached. Example: Note that the select_related() QuerySet method recursively prepopulates the cache of all one-to-many relationships ahead of time. Example:
Topic guide
Pagination
… you methods for accessing the items for each page: NOTE: Note that you can give Paginator a list/tuple, a Django QuerySet, > or any other object with a count() or __len__() method. When > determining the number of objects contained …
Topic guide
Testing tools
Test cases features
See assertJSONEqual() for further details. TransactionTestCase.assertQuerySetEqual(qs,values,transform=None,ordered=True,msg=None) : Asserts that a queryset qs matches a particular iterable of values values. TransactionTestCase.assertNumQueries(num,func,*args,**kwargs) : Asserts that when func is called with *args and **kwargs that num database queries are executed.
Index
| | | | --- | --- | | - handle() (BaseCommand method) - handle_app_config() (AppCommand method) - handle_label() (LabelCommand method) - handle_no_permission() (AccessMixin method) - handle_raw_input() (FileUploadHandler method) - handler400 (in module django.conf.urls) - handler403 (in module django.conf.urls) - …
Index
| | | | --- | --- | | - mail_admins() (in module django.core.mail) - mail_managers() (in module django.core.mail) - make_aware() (in module django.utils.timezone) - make_list - template filter - make_naive() (in module django.utils.timezone) - make_object_list (YearArchiveView attribute) - make_password() …
How-to
How to write custom lookups
You will need to ensure that this registration happens before you try to create any querysets using it. You could place the implementation in a models.py file, or register the lookup in the ready() method of an AppConfig.
Reference
The Django admin site
ModelAdmin objects
ModelAdmin.save_as : Set save_as to enable a “save as new” feature on admin change forms. ModelAdmin.save_as_continue : When save_as=True, the default redirect after saving the new object is to the change view for that object.
Reference
The syndication feed framework
The high-level framework
This simple example, taken from a hypothetical police beat news site describes a feed of the latest five news items: To connect a URL to this feed, put an instance of the Feed object in your URLconf.