2017-11-16 7 views
0

コンテキストは次のとおりです。サッカーの試合にデータを格納するMatchモデルがあります。シーズン、local_team、away_teamなど、すべての試合に共通するフィールドがいくつかあります(フィールド名を意味するものではありません)。しかし、試合の統計は異なるものになります(1試合は2ゴールもう1つは0です)。Djangoの順方向および逆方向のフィールド関係

私のアプローチマッチの共通抽象(Djangoのモデル)を構築するがクラス統計を作成しているとクラスアクション(アクションを保存するため)。だから、:

アクションフィールドを持つことになります - >分、プレーヤー、アクション(目標、レッドカード...)

統計フィールドを持つことになります - >(いくつかのアクションを格納するために、多くの分野に多くの)アクション

試合には、ローカル、アウェイ、レファン、スタジアム、統計などのフィールドがあります(すべての試合に共通のフィールドが1つあります)。

大きな問題は、私はアクションと統計に適切な名前を付けたいとき、今ています。 MatchMountFormが私にゲームの統計情報を求めたら、どれが対応しているのかを素早く知りたいと思っています.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で午前統計フィールドでMATCH の追加]を選択したときに、私はが対応するマッチに格納しようとしていること(STATSを追加することが、私は希望カスタムフィールド名を統計情報の名前フィールドとして保存します。

ありがとうございました!

PR

答えて

0

あなたはあなたの特定のinstanceてバックトレースするrelated_nameを使用します。例えば:Matchクラス、ないインスタンス指す

def __str__(self): 
    return "%s %s %s" % (Match.local_team, Match.away_team) 

^。

代わりに、使用することができます:返信用

def __str__(self): 
    return "{0} {1}".format(self.match_stats.local_team, self.match_stats.away_team) 
+0

こんにちは@Hybrid感謝を! これは関係のエラーをreasingさ: 「例外タイプ:\t RelatedObjectDoesNotExist 例外値:\t統計にはmatch_stats.'を持っていません –

関連する問題