从django model.ForeignKey字段获取django admin中已过滤的对象列表

时间:2012-03-02 21:09:59

标签: django django-models django-admin

我有五个模特:

Class League(models.Model):
    type = models.CharField(max_length=30)

class Team(models.Model):
    name = models.CharField(max_length=30)
    league = models.ForeignKey(max_length=30)

class Player(models.Model):
    name = models.CharField(max_length=30)
    league = models.ForeignKey(Team)

class Fixture(models.Model):
    date = models.DateField();
    league = models.ForeignKey(League)
    team_1 = models.ForeignKey(Team, rel="home")
    team_2 = models.ForeignKey(Team, rel="away")

class Team1_score(models.Model):
    fixture = models.ForiegnKey(Fixture)
    player = models.ForeignKey(Player)
    score = models.IntegerField(max_length=4)

现在在django管理员中,当我编辑有关编辑Team1_score的详细信息时,我只想列出有关团队1的玩家的信息。这就是我在管理中所做的事情

class Team1ScoreInline(models.TabularInline):
    extra = 1

class Fixture(models.Admin):
    inlines = [Team1ScoreInline, Team2ScoreInline]

但是当我选择要添加到记分卡的玩家时,它会向我列出数据库中所有玩家的显示。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Django 1.3+,则可以使用queryset选项,如下所述:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.queryset