我正在创建一个小型网站来注册体育赛事结果。我有两个以下型号:
更详细:
class Team(models.Model):
name = models.CharField(max_length=255)
class Player(models.Model):
team = models.ForeignKey(Team)
name = models.CharField(max_length=255)
total_goals = models.IntegerField(default=0)
def __unicode__(self):
return u"%s (%s)" % (self.name, self.team)
class Match(TimeStampedModel):
competition = models.ForeignKey(Competition)
league = models.ForeignKey(League, null=True, blank=True) # null==True, ha osztalyozo
home = models.ForeignKey(Team, related_name="merkozes_home")
away = models.ForeignKey(Team, related_name="merkozes_away")
home_score = models.SmallIntegerField(blank=True, null=True)
away_score = models.SmallIntegerField(blank=True, null=True)
goal_list = models.ManyToManyField(Player, through='Goal')
@property
def winner(self):
return self.home if self.home_score > self.away_score else self.away
@property
def loser(self):
return self.home if self.home_score < self.away_score else self.away
class Meta:
unique_together = (('league', 'home', 'away'),)
def __unicode__(self):
return u"%s - %s (%s)" % (self.home, self.away, self.competition)
class Goal(TimeStampedModel):
player = models.ForeignKey(Player)
,atch = models.ForeignKey(Match)
goals = models.SmallIntegerField(default=0)
self_goal = models.BooleanField(default=False, blank=True)
我想在django admin下编辑目标以及匹配详情。因此,我创建了以下管理模型:
class GoalsInline(admin.TabularInline):
model = Goal
extra = 1
@admin.register(Match)
class MerkozesAdmin(admin.ModelAdmin):
inlines = [GoalsInline]
奇怪的是,我的管理员内联不显示player
字段,只显示Goal
模型中的其他字段。我也检查了html代码,它甚至都不是隐藏的字段。
您是否知道可能是什么原因以及如何显示所有字段?
答案 0 :(得分:1)
您的GoalsInline
只会显示Player
对象的选择字段,因为它是ForeignKey。
另一种方法是在管理员中注册Player
类,以便在内联中显示加号,以便您可以从该内联添加新的Player
对象。
@admin.register(Player)
class PlayerAdmin(admin.ModelAdmin):
pass