InlineModelAdmin
class InlineModelAdmin
class TabularInline
class StackedInline
举例,有两个Model:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
如果想在Author页面上编辑book:
from django.contrib import admin
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
Django提供了两个InlineModelAdmin的子类:
TabularInline
StackedInline
区别在于使用的模板。
InlineModelAdmin options选项
InlineModelAdmin和ModelAdmin共通的options有:
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()
额外增加的options有:
InlineModelAdmin.model
inline使用的model,必需。
InlineModelAdmin.fk_name
model的name,当有多个外键时使用。
InlineModelAdmin.formset
缺省BaseInlineFormSet。
InlineModelAdmin.form
缺省ModelForm。当创建formset时传递给inlineformset_factory()。
InlineModelAdmin.extra
inline的额外数目。
InlineModelAdmin.get_extra()也返回inline的额外数目。
InlineModelAdmin.max_num
可展示得最大数目。
InlineModelAdmin.get_max_num()也返回此数字。
InlineModelAdmin.min_num
可展示的最小数目。
InlineModelAdmin.get_min_num()也返回此数目。
InlineModelAdmin.raw_id_fields
同ModelAdmin。
class BookInline(admin.TabularInline):
model = Book
raw_id_fields = ("pages",)
InlineModelAdmin.template
使用的模板。
InlineModelAdmin.verbose_name
覆盖meta class中的verbose_name。
InlineModelAdmin.verbose_name_plural
同上
InlineModelAdmin.can_delete
默认为True。
InlineModelAdmin.get_formset(request, obj=None, **kwargs)
参考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)
见上。
多个 ForeignKey链向同一个Model的情况
如果有多个foreign keys:
from django.db import models
class Friendship(models.Model):
to_person = models.ForeignKey(Person, related_name="friends")
from_person = models.ForeignKey(Person, related_name="from_friends")
展示其中一个:
from django.contrib import admin
from myapp.models import Friendship
class FriendshipInline(admin.TabularInline):
model = Friendship
fk_name = "to_person"
class PersonAdmin(admin.ModelAdmin):
inlines = [
FriendshipInline,
]
Working with many-to-many models
model例子:
from django.db import models
class 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 admin
class MembershipInline(admin.TabularInline):
model = Group.members.through
class PersonAdmin(admin.ModelAdmin):
inlines = [
MembershipInline,
]
class GroupAdmin(admin.ModelAdmin):
inlines = [
MembershipInline,
]
exclude = ('members',)
Note:
第一 , the MembershipInline类指向Group.members.through. The through attribute指向管理the many-to-many关系的数据库.
第二,, the GroupAdmin 必须排除the members字段.
Working with many-to-many intermediary models
明确指出中间model的例子:
from django.db import models
class 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)
第一步:
class MembershipInline(admin.TabularInline):
model = Membership
extra = 1
第二步:
class PersonAdmin(admin.ModelAdmin):
inlines = (MembershipInline,)
class GroupAdmin(admin.ModelAdmin):
inlines = (MembershipInline,)
第三步:
admin.site.register(Person, PersonAdmin)
admin.site.register(Group, GroupAdmin)
Using generic relations as an inline
一个inline with generically related objects例子:
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
class 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)
如果想在Product add/change页面上编辑Image实例,可以使用GenericTabularInline或GenericStackedInline:
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from myproject.myapp.models import Image, Product
class ImageInline(GenericTabularInline):
model = Image
class ProductAdmin(admin.ModelAdmin):
inlines = [
ImageInline,
]
admin.site.register(Product, ProductAdmin)
以上就是Djangoadminsite(三)InlineModelAdmin的内容,更多相关内容请关注PHP中文网(www.php.cn)!