2014-10-30 107 views
-1

所以我有以下代码:的Django模型的多个参数

class Team(models.Model): 
    shortName = models.CharField(max_length=255) 
    fullName = models.CharField(max_length=255) 
    desc = models.CharField(max_length=255) 


class Match(models.Model): 
    team1 = models.ForeignKey(Team, related_name='team1') 
    team2 = models.ForeignKey(Team, related_name='team2') 
    start_date = models.DateTimeField('date start') 


class Bet(models.Model): 
    user = models.ForeignKey(User) 
    match = models.ForeignKey(Match) 
    team = models.ForeignKey(Team) 
    transaction = models.ForeignKey(Transaction) 
    pub_date = models.DateTimeField('date published') 

我想是在打赌pooints到TEAM1或TEAM2在比赛上的一个参数,我已经试过如下:

team = models.ForeignKey(Match.team1, Match.team2) 

但是,这给我一个语法错误。什么是正确的方法来做到这一点?

回答

1

您的声明仅指示何种类型的对象填充模型的该属性。在这种情况下,Team国外关键点,所以正确的声明应该是

team = models.ForeignKey(Team) 

。另一方面,上市队两次似乎效率不高,所以你会做的更好,以简单地对哪个队一个选择字段在下注中被选中。举一个例子:

team = models.CharField(max_length=1, choices=(('H', 'Home team'), ('A', 'Away team'))) 

然后您的视图代码将关闭并确定要显示的两个团队中的哪一个。