Jamie Wong
Jamie Wong

Reputation: 18350

Inline Forms in Django 1.1 Admin Panel

How do you display forms for the children of a specific model in the Django Admin Panel?

class Matchup(models.Model):
    name        = models.CharField(max_length=30)
    winner      = models.ForeignKey('players.player',blank=True)        

class Slot(models.Model):
    player  = models.ForeignKey('players.player',blank=True)
    matchup = models.ForeignKey(Matchup)

Each matchup will have two slots - how would I go about displaying forms for both of them in line with the Match.

Basically, I want to have something like this:

Matchup Name:     [         ]
Matchup Winner:   [         ]
--
== Slot 1 ==
|| Slot Player:   [         ]
== Slot 2 ==
|| Slot Player:   [         ]

I realize it may appear that the slot model is useless and should just be replaced by two references to players, but there are various reasons I want to do it this way.

EDIT: removed confusing associations

Upvotes: 0

Views: 233

Answers (1)

milkypostman
milkypostman

Reputation: 3043

from models import *

class SlotInline(admin.StackedInline):
    model = Slot

class MatchupAdmin(admin.ModelAdmin):
    model = Matchup
    inlines = [SlotInline]

admin.site.register(Matchup, MatchupAdmin)

Upvotes: 1

Related Questions