【问题标题】:Forward and Reverse Field Relationships in DjangoDjango中的正向和反向字段关系
【发布时间】:2017-11-16 19:43:06
【问题描述】:

上下文是:我有一个 Match 模型,我想在其中存储足球比赛的数据。有一些字段对所有比赛都是通用的(不是我指的字段名称的值),例如赛季,local_team,away_team...但是比赛的统计数据会有所不同(一场比赛可以有2个进球还有一个 0)。

我的方法构建 Match 的通用抽象(Django 模型)是创建类 Stats(存储动作)和类动作。所以:

动作会有字段 --> 分钟、球员、动作(进球、红牌...)

Stats 会有字段 --> 动作(多对多字段来存储多个动作)

比赛将有字段 --> 本地、客场、裁判、体育场、统计数据(因此所有比赛都有 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(将存储在相应的匹配项中,但我想为 stats 的名称字段保存自定义字段名,以及第二种情况)。

非常感谢各位!

公关

【问题讨论】:

    标签: python django foreign-keys many-to-many relationship


    【解决方案1】:

    您将使用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)
    

    【讨论】:

    • 嗨@Hybrid 感谢您的回复!这是关系中的错误:'异常类型:RelatedObjectDoesNotExist 异常值:Stats 没有 match_stats。`
    猜你喜欢
    • 2018-02-06
    • 2021-01-06
    • 1970-01-01
    • 2014-05-27
    • 2018-07-05
    • 1970-01-01
    • 2010-11-03
    • 2013-10-20
    • 2012-12-25
    相关资源
    最近更新 更多