Topic guide
Making queries
Querying JSONField
… based on a given dictionary key, use that key as the lookup name: Multiple keys can be chained together to form a path lookup: If the key is an integer, it will be interpreted as an index transform in an …
4.2
5.0
5.1
5.2
Internals
Django Deprecation Timeline
The undocumented check_for_test_cookie method in AuthenticationForm will be removed following an accelerated deprecation. Users subclassing this form should remove calls to this method, and instead ensure that their auth related views are CSRF protected, which ensures that cookies are enabled.
5.0
5.1
5.2
6.0
Reference
Model field reference
Relationship fieldsManyToManyField
ForeignKey.related_name : The name to use for the relation from the related object back to this one. It’s also the default value for related_query_name (the name to use for the reverse filter name from the target model).
4.2
5.0
5.1
How-to
How to provide initial data for models
Provide data with fixtures
Django also looks for fixtures in the list of directories provided in the FIXTURE_DIRS setting. To completely prevent default search form happening, use an absolute path to specify the location of your fixture file, e.g. loaddata /path/to/sample.
4.2
5.0
Reference
Model field reference
classField[source] : Field is an abstract class that represents a database table column. Django uses fields to create the database table (db_type()), to map Python types to database (get_prep_value()) and vice-versa (from_db_value()).
Internals
Django Deprecation Timeline
The model and form IPAddressField will be removed. A stub field will remain for compatibility with historical migrations. AppCommand.handle_app() will no longer be supported. RequestSite and get_current_site() will no longer be importable from django.contrib.sites.models.
4.2
5.0
5.1
5.2
dev
Index
… opclasses (Index attribute) - (UniqueConstraint attribute) - open() (FieldFile method) - (File method) - (Storage method) - OpenLayersWidget (class in django.contrib.gis.forms.widgets) - OperationalError - OperationCategory (class in django.db.migrations.operations.base) - optimizemigration - django-admin command - optimizemigration command line option - --check …
How-to
How to use Django’s Content Security Policy
To use nonces in your CSP policy, beside the basic config, you need to: Include the NONCE placeholder value in the CSP settings. This only applies to script-src or style-src directives: Add the csp() context processor to your TEMPLATES setting.
dev
How-to
How to use Django’s CSRF protection
Using CSRF protection with AJAX
… the JavaScript Cookie library to replace getCookie: NOTE: The CSRF token is also present in the DOM in a masked form, but only if > explicitly included using csrf_token in a template. The cookie > contains the canonical, unmasked token. …
Internals
Django Deprecation Timeline
The undocumented BaseForm._html_output() method will be removed. The ability to return a str, rather than a SafeString, when rendering an ErrorDict and ErrorList will be removed. See the Django 4.1 release notes for more details on these changes.
dev
Internals
Django Deprecation Timeline
The model and form IPAddressField will be removed. A stub field will remain for compatibility with historical migrations. AppCommand.handle_app() will no longer be supported. RequestSite and get_current_site() will no longer be importable from django.contrib.sites.models.
4.2
5.0
5.1
6.0
dev
Internals
Organization of the Django Project
Steering council
The voter registration form and roll of voters is maintained by the DSF Board.
Tutorial
Advanced tutorial: How to write reusable apps
A package can be imported with import foo.bar or from foo import > bar. For a directory (like polls) to form a package, it must contain > a special file __init__.py, even if this file is empty.
Tutorial
Writing your first Django app, part 3
… application namespace: polls/urls.py Now change your polls/index.html template from: polls/templates/polls/index.html to point at the namespaced detail view: polls/templates/polls/index.html When you’re comfortable with writing views, read part 4 of this tutorial to learn the basics about form processing and generic views.
Reference
Applications
Initialization process
This concept is known as “lazy evaluation”. django.contrib.admin automatically performs autodiscovery of admin modules in installed applications. To prevent it, change your INSTALLED_APPS to contain 'django.contrib.admin.apps.SimpleAdminConfig' instead of 'django.contrib.admin'. RuntimeWarning: Accessing the database during app initialization is discouraged.
5.0
5.1
5.2
6.0
dev
Reference
System check framework
contrib app checks
Admin checks are all performed as part of the admin tag. The following checks are performed on any ModelAdmin (or subclass) that is registered with the admin site: admin.E001: The value of raw_id_fields must be a list or tuple.
Reference
The Django admin site
AdminSite objects
AdminSite.login_form : Subclass of AuthenticationForm that will be used by the admin site login view. AdminSite.logout_template : Path to a custom template that will be used by the admin site logout view. AdminSite.password_change_form : > New in Django 6.0.
Reference
GEOS API
Geometry ObjectsGEOSGeometry
PreparedGeometry objects are optimized for the contains, intersects, covers, crosses, disjoint, overlaps, touches and within operations. Refer to the Prepared Geometries documentation for more information. GEOSGeometry.srs : Returns a SpatialReference object corresponding to the SRID of the geometry or None.
Reference
Content Security Policy
A CSP nonce (“number used once”) is a unique, random value generated per HTTP response. Django supports nonces as a secure way to allow specific inline <script> or <style> elements to execute without relying on 'unsafe-inline'.
dev
Reference
django-admin and manage.py
Available commands
--format FORMAT Specifies the serialization format of the output. Defaults to JSON. Supported formats are listed in Serialization formats. --indent INDENT Specifies the number of indentation spaces to use in the output.