上下文 是:我有一个匹配模型,我想存储足球比赛的数据。有些字段对所有匹配都是通用的(不是我的意思是字段名称),例如season,local_team,away_team ......但是匹配的统计数据会有所不同(一场比赛可以有2个进球)另一个0)。
构建Match的公共抽象(Django模型)的我的方法 正在创建类Stats(用于存储操作)和Class Action。所以:
行动将有字段 - >分钟,球员,动作(进球,红牌......)
统计信息将包含字段 - >动作(多对多的字段存储多个动作)
匹配将包含字段 - >本地,客场,裁判,体育场,统计(因此所有比赛都有1个共同的领域,即统计数据)。
现在 大问题 ,当我想给行动和统计数据指定一个正确的名称时。因为来自不同匹配的很多动作和统计数据将被存储在一起,所以当de MatchAdminForm询问我游戏的统计数据时,我想快速知道哪一个对应,当StatsAdminForm询问我的动作时也一样。
统计数据的理想选择是捕获此统计信息所属匹配的local_team的名称。 How can I access to that name if Match has not been created yet??
(与动作和统计数据相同的并行度)
在这里,我分享了我的models.py,以便您查看。
from django.db import models
from django.utils import timezone
from globalModels.models import Season
from seasonalModels.models import SeasonTeam as Team
from seasonalModels.models import SeasonPlayer as Player
from seasonalModels.models import SeasonReferee as Referee
from seasonalModels.models import SeasonStadium as Stadium
class Action(models.Model):
ACTION_CHOICES = (
('Gol', 'Gol'),
('Yellow', 'Yellow Card'),
('2Yellow', 'Second Yellow Card'),
('Red', 'Red Card'),
)
# minute = models.IntegerField(choices = (range(1,90)))
name = models.CharField(max_length=255, default='abc')
minute = models.IntegerField(default=0)
player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='player_action')
action = models.CharField(choices=ACTION_CHOICES, max_length=255)
def __str__(self):
return "%s %s %s" % (Match.local_team, Match.away_team) # Ideally I will like the stat to give this information BUT MATCH DOESN'T EXISTE YET error
class Stats(models.Model):
actions = models.ManyToManyField(Action, related_name='action_name')
name = models.CharField(max_length=255, default='auto')
def __str__(self):
return "%s %s %s" % (Match.local_team, Match.away_team) # Ideally I will like the stat to give this information BUT MATCH DOESN'T EXISTE YET error
class Match(models.Model):
season = models.ForeignKey(Season, on_delete=models.CASCADE, related_name='match_season')
date = models.DateTimeField(default=timezone.now)
# round = models.IntegerField(choices = (range(1,10)))
round = models.IntegerField(default=0)
number = models.IntegerField(default=0, unique=True)
local_team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='match_local_team')
away_team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='match_away_team')
referee = models.ForeignKey(Referee, on_delete=models.CASCADE, related_name='match_referee')
stadium = models.ForeignKey(Stadium, on_delete=models.CASCADE, related_name='match_stadium')
local_team_possession = models.IntegerField(blank=True, null=True)
away_team_possession = models.IntegerField(blank=True, null=True)
stats = models.OneToOneField(Stats, blank=True, related_name='match_stats')
local_team_goals = models.IntegerField(blank=True, null=True)
away_team_goals = models.IntegerField(blank=True, null=True)
local_team_yellow_cards = models.IntegerField(blank=True, null=True)
local_team_red_cards = models.IntegerField(blank=True, null=True)
away_team_yellow_cards = models.IntegerField(blank=True, null=True)
away_team_red_cards = models.IntegerField(blank=True, null=True)
总结一下,当我在管理浏览器API中并选择ADD MATCH时 在统计信息字段中,我能够添加STATS(将存储在相应的匹配中,但我想为stats的名称字段保存自定义字段名,以及第二种情况)。
非常感谢你们!
PR
答案 0 :(得分:0)
您可以使用related_name
追溯您的特定instance
。例如:
def __str__(self):
return "%s %s %s" % (Match.local_team, Match.away_team)
^指的是Match
类,而不是实例。
相反,您可以使用:
def __str__(self):
return "{0} {1}".format(self.match_stats.local_team, self.match_stats.away_team)