我有以下模型:
class CollegeResult(models.Model):
id = models.IntegerField(primary_key=True)
college_code = models.CharField(max_length=255, blank=True, null=True)
college_name = models.CharField(max_length=255, blank=True, null=True)
branch_name = models.CharField(max_length=255, blank=True, null=True)
cutoff_type = models.CharField(max_length=255, blank=True, null=True)
stage_mark = models.IntegerField(blank=True, null=True)
stage_rank = models.IntegerField(blank=True, null=True)
我想从学院代码,分支名称的组中列出具有max(stage_mark)的行。
我尝试过的方法:
query1 = CollegeResult.objects.filter(cutoff_type__in=quota)
query2 = query1.values('college_code', 'branch_name')
.annotate(stage_mark=Max('stage_mark'))
query1 = CollegeResult.objects.filter(cutoff_type__in=quota)
query2 = query1.values('college_code','branch_name')
.annotate(stage_mark=Max('stage_mark'))
q_statement = Q()
for college in query2:
q_statement |= (Q(college_code=college['college_code'])
& Q(branch_name=college['branch_name'])
& Q(stage_mark=college['stage_mark'])
)
query3 = query1.filter(q_statement)
这给 django.db.utils.OperationalError:SQL变量过多
我应该怎么做?