我有以下结构:
class Player(models.Model):
name = models.CharField(max_length=50)
dob = models.DateField(max_length=80)
class Game(models.Model):
name = models.CharField(max_length=50)
class Attempts(models.Model):
player = models.ForeignKey("Player")
game = models.ForeignKey("Game")
won = models.BooleanField()
班级尝试记录每个玩家的尝试以及他是否赢了或输了一场游戏。现在我想让一些人在agr组中尝试特定游戏。例如0-18岁的人尝试过游戏5我在django用户中咨询了以下这个post并制作了以下代码:
ranges = [(0,18),(18,25),(25,50)]
now = datetime.now()
groups = {}
for (a,b) in ranges:
# print str(a) + " " +str(b)
newer_date = (now - relativedelta(years=a)).date()
older_date = (now - relativedelta(years=b)).date()
print str(newer_date)
print str(older_date)
groups[str(a)+","+str(b)] = Attempts.objects.filter(game__pk = "101",player__dob__range=(older_date,newer_date)).count()
这也正确地给出了结果:
{"0,18": 2, "25,50": 1, "18,25": 1}
此代码所做的查询如何等于范围内的类别数。我想了解如何在一个ORM查询中获取此数据。是否可以这样做?