ModelAdmin - 多对多内联

时间:2011-11-19 03:06:12

标签: django django-modeladmin

好的,我有一个看起来像这样的模型:

class OpHour( models.Model ):
    days_of_the_week =((0,"Sunday"),
                   (1,"Monday"),
                   (2,"Tuesday"),
                   (3,"Wednesday"),
                   (4,"Thursday"),
                   (5,"Friday"),
                   (6,"Saturday"))
    day = models.IntegerField(max_length=1, choices=days_of_the_week)
    opening_time = models.TimeField()
    closing_time = models.TimeField()

class Location( models.Model ):
    name = models.CharField(max_length=200)
    [...]
    hours = models.ManyToManyField(OpHour)

我希望在位置的更改页面中显示和编辑OpHour作为内联。如何使用ModelAdmin实现此目的?

1 个答案:

答案 0 :(得分:1)

只是想在此处复制并粘贴文档的相关部分。 @Paulo发布了一个链接,但您应该特别注意the docs中的"through"

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',)