Django admin site (iii) InlineModelAdmin, djangoadmin
InlineModelAdmin
Class InlineModelAdmin
Class TabularInline
Class StackedInline
For example, there are two models:
from django.db import modelsclass Author(models.Model): name = models.CharField(max_length=100)class Book(models.Model): author = models.ForeignKey(Author) title = models.CharField(max_length=100)
To edit a book on the Author page:
from django.contrib import adminclass BookInline(admin.TabularInline): model = Bookclass AuthorAdmin(admin.ModelAdmin): inlines = [ BookInline, ]
Django provides two subclasses of InlineModelAdmin:
TabularInline
StackedInline
The difference is the template used.
InlineModelAdmin options
The options that InlineModelAdmin and ModelAdmin share are:
Form
Fieldsets
Fields
Formfield_overrides
Exclude
Filter_horizontal
Filter_vertical
Ordering
Prepopulated_fields
Get_queryset ()
Radio_fields
Readonly_fields
Raw_id_fields
Formfield_for_choice_field ()
Formfield_for_foreignkey ()
Formfield_for_manytomany ()
Has_add_permission ()
Has_change_permission ()
Has_delete_permission ()
The additional options include:
InlineModelAdmin. model
The model used by inline. required.
InlineModelAdmin. fk_name
Name of the model, which is used when multiple foreign keys exist.
InlineModelAdmin. formset
The default value is BaseInlineFormSet.
InlineModelAdmin. form
Default ModelForm. When formset is created, it is passed to inlineformset_factory ().
InlineModelAdmin. extra
The additional number of inline.
InlineModelAdmin. get_extra () also returns the additional number of inline.
InlineModelAdmin. max_num
The maximum number displayed.
This number is also returned for InlineModelAdmin. get_max_num.
InlineModelAdmin. min_num
The minimum number of items that can be displayed.
InlineModelAdmin. get_min_num () also returns this number.
InlineModelAdmin. raw_id_fields
Same as ModelAdmin.
class BookInline(admin.TabularInline): model = Book raw_id_fields = ("pages",)
InlineModelAdmin. template
Template used.
InlineModelAdmin. verbose_name
Overwrite verbose_name in meta class.
InlineModelAdmin. verbose_name_plural
Same as above
InlineModelAdmin. can_delete
The default value is True.
InlineModelAdmin. get_formset (request, obj = None, ** kwargs)
See ModelAdmin. get_formsets_with_inlines.
InlineModelAdmin. get_extra (request, obj = None, ** kwargs)
class BinaryTreeAdmin(admin.TabularInline): model = BinaryTree def get_extra(self, request, obj=None, **kwargs): extra = 2 if obj: return extra - obj.binarytree_set.count() return extra
InlineModelAdmin. get_max_num (request, obj = None, ** kwargs)
class BinaryTreeAdmin(admin.TabularInline): model = BinaryTree def get_max_num(self, request, obj=None, **kwargs): max_num = 10 if obj.parent: return max_num - 5 return max_num
InlineModelAdmin. get_min_num (request, obj = None, ** kwargs)
See.
When multiple ForeignKey chains are directed to the same Model
If there are multiple foreign keys:
from django.db import modelsclass Friendship(models.Model): to_person = models.ForeignKey(Person, related_name="friends") from_person = models.ForeignKey(Person, related_name="from_friends")
Show one of them:
from django.contrib import adminfrom myapp.models import Friendshipclass FriendshipInline(admin.TabularInline): model = Friendship fk_name = "to_person"class PersonAdmin(admin.ModelAdmin): inlines = [ FriendshipInline, ]
Working with role-to-Role models
Model example:
from django.db import modelsclass Person(models.Model): name = models.CharField(max_length=128)class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, related_name='groups')
Inlines:
from django.contrib import adminclass MembershipInline(admin.TabularInline): model = Group.members.throughclass PersonAdmin(admin.ModelAdmin): inlines = [ MembershipInline, ]class GroupAdmin(admin.ModelAdmin): inlines = [ MembershipInline, ] exclude = ('members',)
Note:
First, the MembershipInline class points to The Group. members. through. the through attribute points to the database that manages The role-to-role relationship.
Second, the GroupAdmin must exclude the members field.
Working with example-to-example intermediary models
Example of intermediate model:
from django.db import modelsclass Person(models.Model): name = models.CharField(max_length=128)class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership')class Membership(models.Model): person = models.ForeignKey(Person) group = models.ForeignKey(Group) date_joined = models.DateField() invite_reason = models.CharField(max_length=64)
Step 1:
class MembershipInline(admin.TabularInline): model = Membership extra = 1
Step 2:
class PersonAdmin(admin.ModelAdmin): inlines = (MembershipInline,)class GroupAdmin(admin.ModelAdmin): inlines = (MembershipInline,)
Step 3:
admin.site.register(Person, PersonAdmin)admin.site.register(Group, GroupAdmin)
Using generic relations as an inline
An example of inline with generically related objects:
from django.db import modelsfrom django.contrib.contenttypes.fields import GenericForeignKeyclass Image(models.Model): image = models.ImageField(upload_to="images") content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey("content_type", "object_id")class Product(models.Model): name = models.CharField(max_length=100)
To edit an Image instance on the Product add/change page, use GenericTabularInline or GenericStackedInline:
from django.contrib import adminfrom django.contrib.contenttypes.admin import GenericTabularInlinefrom myproject.myapp.models import Image, Productclass ImageInline(GenericTabularInline): model = Imageclass ProductAdmin(admin.ModelAdmin): inlines = [ ImageInline, ]admin.site.register(Product, ProductAdmin)
Why can't I execute django-adminpy startproject mysite? In the command window, the system prompts: it is not an internal or external command.
Python is not included in the path environment variable. Add the python installation directory such as python32 to the path. In this way, you can enter the django-admin.py startproject mysite in the command line.
Deploy django17 to mod_wsgi and enter the admin interface without the css style. The django built-in server can be used. How can this problem be solved?
Copy this folder: Python installation path \ Lib \ site-packages \ django \ contrib \ admin \ static file to the project, add STATIC_DIRS = (path of the folder) to the setting file of the project.