2016-11-20 35 views
0

我編碼在Django的大型體育聯盟。我有單獨的體育,團隊,學校和時間表。時間表模型包括主頁和離開分數。這是一個基於結果的應用程序,而不是調度程序。寫一個體育聯盟計劃「Django的方式」

我的問題是關於記錄結果「Django的方式」。我可以創建節約勝損失結果的模式,但感覺就像存儲不必要的數據,如果兩個用戶爲同一遊戲不同的提交分數可能是一個問題。另一種方法是使用查詢並在每次調用時計算獲勝/失敗。

我準備好迎接挑戰,但我擔心,我會開始走向死衚衕去。這裏是我的模型,如果有幫助:

from django.db import models 
from django.contrib import admin 

class Sports(models.Model): 
    sport = models.AutoField(primary_key=True) 
    sport_name = models.CharField(max_length=225, blank=True, null=True) 

    class Meta: 
     managed = True 
     db_table = 'sports' 
    def __str__(self): 
     return self.sport_name 


class School(models.Model): 
    school = models.AutoField(primary_key=True) 
    school_name = models.CharField(max_length=225, blank=True, null=True) 

    class Meta: 
     managed = True 
     db_table= 'school' 
    def __str__(self): 
     return self.school_name 

class SchoolAdmin(admin.ModelAdmin): 
    fields = ('school_id', 'school_name') 

class Teams(models.Model): 
    team = models.AutoField(primary_key=True) 
    team_name = models.CharField(max_length=225, blank=True, null=True) 
    sport_id = models.ForeignKey(Sports, models.DO_NOTHING, blank=True, null=True) 
    division = models.CharField(max_length=225, blank=True, null=True) 
    school = models.ForeignKey(School, models.DO_NOTHING, blank=True, null=True) 

    class Meta: 
     managed = True 
     db_table = 'teams' 
    def __str__(self): 
     return self.team_name 

class Schedule(models.Model): 
    match = models.AutoField(primary_key=True) 
    match_date = models.DateField(blank=True, null=True) 
    home = models.ForeignKey(Teams, related_name='home_set', blank=True, null=True) 
    away = models.ForeignKey(Teams, related_name='away_set', blank=True, null=True) 
    home_score = models.IntegerField(blank=True, null=True) 
    away_score = models.IntegerField(blank=True, null=True) 
    class Meta: 
     managed = True 
     db_table = 'schedule' 
    def __str__(self): 
     return '%s at %s' % (self.away, self.home) 

class ScheduleAdmin(admin.ModelAdmin): 
    model = Schedule 
    list_display = ('match_date', 'home', 'away', 'home_score', 'away_score') 

回答

0

創建儘可能多的模型,你需要。如果你只需要目標/分/得分的數量,那麼你可以將它保存到一個字段上的博弈模型。如果您需要關於這些目標/分數的額外數據,請爲這些數據創建一個單獨的模型。是不是真的有一個正確或錯誤的答案,這個 - 你只需要想想你會需要。還要弄清楚你將來可能需要什麼以及如何保存所有這些數據。根據您的需求創建模型,而不是其他人的意見。

我前段時間爲冰球編寫了一個應用程序,我們有很多很多不同的模型,因爲我們需要所有這些數據。我的應用程序的主要模型是季節,裁判,遊戲,GamePlayer,玩家,GameReferee,目標,懲罰,GamePunishment和GoalKeeper,但是,如果您不需要所有這些信息,則不需要太多模型。