API Reference

Applications System check framework Built-in class-based views API Clickjacking Protection contrib packages Cross Site Request Forgery protection Databases django-admin and manage.py Running management commands from your code Django Exceptions File handling Forms Middleware Migration Operations Models Request and response objects SchemaEditor Settings Signals Templates TemplateResponse and SimpleTemplateResponse Unicode data django.urls utility functions django.conf.urls utility function

Aggregation

The topic guide on Django’s database-abstraction API described the way that you can use Django queries that create, retrieve, update and delete individual objects. However, sometimes you will need to retrieve values that are derived by summarizing or aggregating a collection of objects. This topic guide describes the ways that aggregate values can be generated and returned using Django queries. Throughout this guide, we’ll refer to the following models. These models are used to track the invent

Advanced tutorial: How to write reusable apps

This advanced tutorial begins where Tutorial 7 left off. We’ll be turning our Web-poll into a standalone Python package you can reuse in new projects and share with other people. If you haven’t recently completed Tutorials 1–7, we encourage you to review these so that your example project matches the one described below. Reusability matters It’s a lot of work to design, build, test and maintain a web application. Many Python and Django projects share common problems. Wouldn’t it be great if we

admin.views.decorators.staff_member_required()

staff_member_required(redirect_field_name='next', login_url='admin:login') [source] This decorator is used on the admin views that require authorization. A view decorated with this function will having the following behavior: If the user is logged in, is a staff member (User.is_staff=True), and is active (User.is_active=True), execute the view normally. Otherwise, the request will be redirected to the URL specified by the login_url parameter, with the originally requested path in a query str

admin.TabularInline

class TabularInline [source]

admin.StackedInline

class StackedInline [source] The admin interface has the ability to edit models on the same page as a parent model. These are called inlines. Suppose you have these two models: from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) title = models.CharField(max_length=100) You can edit the books authored by an author on the author page. You add inlines t

admin.register()

register(*models, site=django.admin.sites.site) [source] There is also a decorator for registering your ModelAdmin classes: from django.contrib import admin from .models import Author @admin.register(Author) class AuthorAdmin(admin.ModelAdmin): pass It is given one or more model classes to register with the ModelAdmin and an optional keyword argument site if you are not using the default AdminSite: from django.contrib import admin from .models import Author, Reader, Editor from myproje

admin.models.LogEntry.user

LogEntry.user The user (an AUTH_USER_MODEL instance) who performed the action.

admin.models.LogEntry.object_repr

LogEntry.object_repr The object`s repr() after the modification.

admin.models.LogEntry.object_id

LogEntry.object_id The textual representation of the modified object’s primary key.