Django-异构查询
以这种模式为例:Django-异构查询,django,inheritance,orm,Django,Inheritance,Orm,以这种模式为例: SoccerMatch(Model): """ FIFA World Cup 2014 match. It could have no teams yet if it's scheduled but a shootout stage (they are never defined until both previous winner are determined) """ #this is defined beforehand
SoccerMatch(Model):
"""
FIFA World Cup 2014 match. It could have no teams yet
if it's scheduled but a shootout stage (they are never
defined until both previous winner are determined)
"""
#this is defined beforehand
date = DateTimeField()
#this is defined beforehand -for group matches- and when
#the previous winners are determined -for shootout matches-
team1 = ForeignKey(Team, related_name="+", null=True)
team2 = ForeignKey(Team, related_name="+", null=True)
#these are defined when the match ends
goals1 = PositiveSmallIntegerField(null=True, blank=True)
goals2 = PositiveSmallIntegerField(null=True, blank=True)
GroupSoccerMatch(SoccerMatch):
"""
FIFA World Cup 2014 match for first stage.
Each group has 4 teams and plays 6 matches.
"""
group = ForeignKey(Group, related_name="matches")
ShootoutSoccerMatch(SoccerMatch):
"""
FIFA World Cup 2014 match for second stage.
Second stage plays 16 matches in rounds of
8, 4, 2, 1 (3rd and 4th) and 1 (1st and 2nd).
"""
#... more fields related to the shootout hierarchy
#....those fields are omitted since they don't matter here
#this is defined when the match ends
penalty_shootout = PositiveSmallIntegerField(null=True, blank=True, choices=((1, "Team 1 won"), (2, "Team 2 won")))
在这段代码中,我省略了团队和小组的定义,因为它与涉及的数据无关,与我为世界杯足球比赛建模的问题无关,目前只针对本次世界杯,我想对比赛结果进行预测
考虑到前面的模型有自己的清理方法
此应用程序可以配置为具有精确的预测,其中您必须猜测两个球队的进球以及罚球回合的获胜者(如果存在),或者具有不精确的预测,其中您必须猜测进球数中的获胜者,或者如果进球数相等且罚球回合存在,则猜测该罚球回合的获胜者
在这两种情况下,我必须考虑惩罚回合,这在前48场比赛中是不存在的,它根本就不存在。
我的预测模型课是:
class Prediction(Model)
user = ForeignKey(User, related_name="predictions")
match = ForeignKey(SoccerMatch, related_name="predictions")
goals1 = PositiveSmallIntegerField(null=True, blank=True)
goals2 = PositiveSmallIntegerField(null=True, blank=True)
penalty_shootout = PositiveSmallIntegerField(null=True, blank=True, choices=((1, "Team 1 won"), (2, "Team 2 won")))
考虑最简单的情况:精确预测——一个好的预测有goals1=match.goals1,goals2=match.goals2,点球决战=match.pould\u决战
我想我可以解决这样的精确预测:
#1 we consider predictions from a specific user
done_predictions = Prediction.objects.filter(user=anUser)
#2 we consider resolved predictions (i.e. involving matches played)
resolved_predictions = done_predictions.filter(match__date__lt=now() + timedelta(hours=1, minutes=30), match__goals1__isnull=False, match__goals2__isnull=False)
#3 we consider accurate predictions
accurate_predictions = resolved_predictions.filter(goals2=F('match__goals2'), goals1=F('match__goals1'))
现在一切都很好,但是我有一个问题:我想考虑最后16场比赛的情况,我必须考虑罚点球阶段的机会。
有没有一种方法可以修复第二和第三查询来考虑这种情况而不是两个单独的查询?这意味着:无法查询match\uu Penal\u shootout=因为SoccerMatch没有该字段。或者:有没有办法为匹配的组声明虚拟字段Penal\u shootout,该组的虚拟值为None?该死。找到了解决方法 Django允许通过查询参数查找遍历派生属性,例如管理器,在Django 1.7中,还允许遍历自定义转换 对于同一个问题,我的逻辑是确定点球决战定义为1还是2是平局决战比赛的唯一允许值,还是不允许 如果比赛不是射击赛,而是团体赛,则该比赛记录将不存在射击赛片段。因此,当查询match\uuuu shootoutsoccermatch\uuuuu population\u shootout时,如果值未指定,即没有平局或shootout子类记录不存在,我将不会得到任何结果。因此,在我的示例中,查询match\uuuu shootoutsoccermatch\uuuu penal\u shootout\uuuuu isnull=True和match\uuuu goals1=F'match\uuuuu goals2'我指定了平局,并且由于干净的实现,没有任何一个结果意味着什么,但比赛不是枪战