Fixed #35887 -- Added imports and admin.site.register to non-partial admin inline doc examples.

This commit is contained in:
Alexander Lazarević 2024-11-13 09:01:57 +01:00 committed by Sarah Boyce
parent 512a2bad05
commit 8590d05d44
2 changed files with 20 additions and 7 deletions

View File

@ -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``.

View File

@ -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.