我正在使用django 1.5.5和Mysql
我有这个模特
class Student(models.Model):
username = models.CharField(max_length=200,unique = True)
class Score(models.Model)
student = models.ForeignKey(Student)
date = models.DateTimeField()
score = models.IntegerField()
我希望获得得分为0的所有得分记录,并且同一学生还有0。
我尝试了什么:
scores = Score.objects.annotate(score_count = Count('student__id')).filter(score = 0 , score_count__gt = 1)
另一件事,我尝试了这个,我认为确实能得到我需要的东西,但它需要2个查询。
students = Score.objects.filter(score = 0).values('student__id').annotate(c=Count('student__id')).filter(c__gt=1).values_list('student__id',flat=True)
score = Score.objects.filter(score = 0 , student__id__in = students)
在一个查询中以任何方式执行此操作?
答案 0 :(得分:0)
no_of_records = len(Score.objects.values('student__id').filter(score=0).annotate(c=Count('score')).filter(c__gt=1))
OR
no_of_records = len(Score.objects.values('student__id').filter(score=0).annotate(c=Count('score')).filter(c__gt=1)).count()