【发布时间】:2016-07-18 01:09:43
【问题描述】:
我正在为我的兄弟会编写一个 Django 应用程序来对 rushees 进行投票,我正在尝试优化我的一个查询,该查询计算选票并打印出计数以及来自他们的应用程序的信息。 Django-debug 工具栏告诉我我有很多重复的查询
(为清楚起见,以下代码已被截断和编辑)
models.py
votechoices = ((1, "Yes"),(2, "No"),(3, "Abstain"))
class Vote(models.Model):
brother = models.ForeignKey(Brother)
rushee = models.ForeignKey(Rushee)
choice = models.IntegerField(choices=votechoices, default=3)
class Rushee(models.Model):
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
#ETC, ETC
class Application(models.Model):
rushee = models.ForeignKey(Rushee)
address = models.CharField(max_length=200)
cellphone = models.CharField(max_length=30)
#ETC, ETC
views.py
def getvotecount(request):
# get all the applications ( we only vote on people who have an application)
applicationobjs = Application.objects.select_related('rushee').all()
# iterate through the applications and count the votes
for app in applicationobjs.iterator():
#>>>> This Query below is a seperate query everytime! So that means that If we have 200 rushees there are 200 queries!
counts = Vote.objects.filter(rushee=app.rushee).values('choice').annotate(count=Count('choice'))
votesfor = sum([x['count'] for x in counts if x['choice'] == 1])
votesagainst = sum([x['count'] for x in counts if x['choice'] == 2])
result = [app.rushee.first_name + ' ' + app.rushee.last_name,
app.address, app.cellphone,
str(votesfor), str(votesagainst),]
# Uninteresting stuff below that will group together and return the results
我正在尝试优化由 (>>>>) 标记的视图中的查询,以便我可以返回每个 rushee 的票数,而无需每次都运行单独的查询!
附加信息: sqlite 后端,rushees 比应用程序多得多,我们只对有应用程序的 rushees 投票
【问题讨论】: