Django,计算复杂查询的结果会得到错误的结果

时间:2014-02-27 22:17:50

标签: django

运行通过计算注释进行过滤的查询会返回正确的结果,但是当我尝试计算按街道名称分组的查询结果时,计数​​不正确。我尝试删除order_by,但这无关紧要,因为'street_name'也在我正在创建的values_list中。

class Voter(models.Model):
   ...
   street_name = models.CharField(max_length=64)
   precinct    = models.PositiveSmallIntegerField(blank=True, null=True)
   ...

class VoterHistory(models.Model):
   reg_number = models.ForeignKey(Voter)
   election_type = models.CharField(max_length=8)

# The voter query works correctly on its own.
voters = Voter.objects.filter(
         # VoterHistory filters
         # Voters that have voted more than once with an election_type of PRI
            voterhistory__election_type__startswith = "PRI"
         ).annotate(
            election_count=Count('voterhistory__election_type')
         ).filter(
            election_count__gte=1

         # extra filters
         ).filter(
            precinct=10
         )

# Counting does not work if the voter history filters are included in the voter query
counts = voters.values_list('street_name').annotate(Count('street_name')).order_by('street_name')

0 个答案:

没有答案