0

Okay, so I have a model that looks like this:

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)

and I would like to display and edit OpHour as an inline in Location's change page. How do I achieve this using ModelAdmin?

George Griffin
  • 634
  • 7
  • 17
  • helpful https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model – Paulo Nov 19 '11 at 04:31

1 Answers1

1

Just wanted to copy and paste the relevant part the docs here. @Paulo posted a link, but you should look specifically at the "through" in 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',)
snakesNbronies
  • 3,619
  • 9
  • 44
  • 73