依赖领域的订单模型

时间:2016-09-01 03:49:26

标签: python django field models

我有2个型号,PlayerPick和Game。 PlayerPick拥有游戏模型的外键,显示选择的游戏。如果我想在游戏时间之前订购所有选秀权,最好的方法是什么?有没有办法自动将gameTime字段添加到玩家选择模型?请参阅以下代码

class PlayerPick(models.Model):
    player_profile = models.ForeignKey('PlayerProfile')
    team = models.ForeignKey('Team')

class Game(models.Model):
    team1 = models.ForeignKey('Team', related_name="game_set_team1")
    team2 = models.ForeignKey('Team', related_name="game_set_team2")
    time = models.DateTimeField(null=True, blank=True)

1 个答案:

答案 0 :(得分:0)

class PlayerPick(models.Model):
    player_profile = models.ForeignKey('PlayerProfile')
    team = models.ForeignKey(Game)

class Game(models.Model):
    team1 = models.ForeignKey('Team', related_name="game_set_team1")
    team2 = models.ForeignKey('Team', related_name="game_set_team2")
    time = models.DateTimeField(null=True, blank=True)

你可以这样做

PlayerPick.objects.all().order_by(team = Game.objects.all().order_by('time'))

但您可以随时定义自定义模型管理器,如果您定期访问该查询,它将使您的生活更轻松。请检查: http://blog.kevinastone.com/django-model-behaviors.html