From 8590d05d44a4f3df56d988229e43d66c37df79da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Lazarevi=C4=87?= Date: Wed, 13 Nov 2024 09:01:57 +0100 Subject: [PATCH] Fixed #35887 -- Added imports and admin.site.register to non-partial admin inline doc examples. --- docs/ref/contrib/admin/index.txt | 23 ++++++++++++++++------- docs/topics/db/multi-db.txt | 4 ++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index d9e12f0165..1b02b7d403 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2251,6 +2251,7 @@ information. inlines to a model by specifying them in a ``ModelAdmin.inlines``:: from django.contrib import admin + from myapp.models import Author, Book class BookInline(admin.TabularInline): @@ -2262,6 +2263,9 @@ information. BookInline, ] + + admin.site.register(Author, AuthorAdmin) + Django provides two subclasses of ``InlineModelAdmin`` and they are: * :class:`~django.contrib.admin.TabularInline` @@ -2494,6 +2498,10 @@ Take this model for instance:: from django.db import models + class Person(models.Model): + name = models.CharField(max_length=128) + + class Friendship(models.Model): to_person = models.ForeignKey( Person, on_delete=models.CASCADE, related_name="friends" @@ -2507,7 +2515,7 @@ you need to explicitly define the foreign key since it is unable to do so automatically:: from django.contrib import admin - from myapp.models import Friendship + from myapp.models import Friendship, Person class FriendshipInline(admin.TabularInline): @@ -2520,6 +2528,9 @@ automatically:: FriendshipInline, ] + + admin.site.register(Person, PersonAdmin) + Working with many-to-many models -------------------------------- @@ -2548,24 +2559,22 @@ If you want to display many-to-many relations using an inline, you can do so by defining an ``InlineModelAdmin`` object for the relationship:: from django.contrib import admin + from myapp.models import Group class MembershipInline(admin.TabularInline): model = Group.members.through - class PersonAdmin(admin.ModelAdmin): - inlines = [ - MembershipInline, - ] - - class GroupAdmin(admin.ModelAdmin): inlines = [ MembershipInline, ] exclude = ["members"] + + admin.site.register(Group, GroupAdmin) + There are two features worth noting in this example. Firstly - the ``MembershipInline`` class references ``Group.members.through``. diff --git a/docs/topics/db/multi-db.txt b/docs/topics/db/multi-db.txt index be7e9953fa..11be913f4c 100644 --- a/docs/topics/db/multi-db.txt +++ b/docs/topics/db/multi-db.txt @@ -683,6 +683,10 @@ Once you've written your model admin definitions, they can be registered with any ``Admin`` instance:: from django.contrib import admin + from myapp.models import Author, Book, Publisher + + # Import our custom ModelAdmin and TabularInline from where they're defined. + from myproject.admin import MultiDBModelAdmin, MultiDBTabularInline # Specialize the multi-db admin objects for use with specific models.