2011-11-19 67 views
0

好了,所以我有一個模型,看起來像這樣:的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實現此目的?

+0

有益https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-a-在"through"專門研究模型有二個或更多的外國密鑰對的 - 同父模型 – Paulo

回答

1

只是想在這裏複製和粘貼相關部分的文檔。 @Paulo發佈一個鏈接,但你應該在the docs

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