问题来自您的list(reviews)
表达 - 这迫使 RawQuerySet 尝试以一种或另一种方式从初始查询构建完整的模型实例(可能不是最有效的方式,但这是另一回事)。如果没有此行并假设您没有访问相关对象,则只会进行一个查询,如您在此代码段中所见:
>>> from survey.models import Question
>>> from django.db import connection
>>> import pprint
>>> connection.queries
[]
>>> raw = Question.objects.raw("select * from survey_question")
>>> for q in raw:
... print q.id
...
1
2
3
4
>>> connection.queries
[{'time': '0.000', 'sql': 'select * from survey_question'}]
>>> list(raw)
[<Question: Survey Essai1 (root) question #1 - Depuis combien de temps programmez vous ?>, <Question: Survey Essai1 (root) question #2 - Comment avez vous débuté ?>, <Question: Survey Essai1 (root) question #3 - Quel est votre niveau de formation>, <Question: Survey Essai1 (root) question #4 - Cette formation porte-t-elle sur l'informatique ?>]
>>> pprint.pprint(connection.queries)
[{'sql': 'select * from survey_question', 'time': '0.000'},
{'sql': 'select * from survey_question', 'time': '0.000'},
{'sql': 'SELECT `survey_survey`.`id`, `survey_survey`.`user_id`, `survey_survey`.`title`, `survey_survey`.`notes`, `survey_survey`.`description`, `survey_survey`.`instructions`, `survey_survey`.`date_created`, `survey_survey`.`starts_on`, `survey_survey`.`ends_on` FROM `survey_survey` WHERE `survey_survey`.`id` = 1 ',
'time': '0.001'},
{'sql': 'SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` WHERE `auth_user`.`id` = 1 ',
'time': '0.001'},
{'sql': 'SELECT `survey_survey`.`id`, `survey_survey`.`user_id`, `survey_survey`.`title`, `survey_survey`.`notes`, `survey_survey`.`description`, `survey_survey`.`instructions`, `survey_survey`.`date_created`, `survey_survey`.`starts_on`, `survey_survey`.`ends_on` FROM `survey_survey` WHERE `survey_survey`.`id` = 1 ',
'time': '0.000'},
{'sql': 'SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` WHERE `auth_user`.`id` = 1 ',
'time': '0.000'},
{'sql': 'SELECT `survey_survey`.`id`, `survey_survey`.`user_id`, `survey_survey`.`title`, `survey_survey`.`notes`, `survey_survey`.`description`, `survey_survey`.`instructions`, `survey_survey`.`date_created`, `survey_survey`.`starts_on`, `survey_survey`.`ends_on` FROM `survey_survey` WHERE `survey_survey`.`id` = 1 ',
'time': '0.000'},
{'sql': 'SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` WHERE `auth_user`.`id` = 1 ',
'time': '0.000'},
{'sql': 'SELECT `survey_survey`.`id`, `survey_survey`.`user_id`, `survey_survey`.`title`, `survey_survey`.`notes`, `survey_survey`.`description`, `survey_survey`.`instructions`, `survey_survey`.`date_created`, `survey_survey`.`starts_on`, `survey_survey`.`ends_on` FROM `survey_survey` WHERE `survey_survey`.`id` = 1 ',
'time': '0.000'},
{'sql': 'SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` WHERE `auth_user`.`id` = 1 ',
'time': '0.000'}]
>>>
现在的重点是:您为什么要使用 RawQueryset 进行上述计算?
我假设avg_rating = avg_rating/float(count)
循环中的第一行 - 是一个复制粘贴错误(如果不是,我很想知道更多关于你对“平均”的定义)。然后你只需要一个查询(不需要循环也不需要 python 计算)来要求数据库进行计算。您可以使用原始 sql 执行此操作:
select avg(stars) from reviews;
或使用 ORM 的聚合函数(参见https://docs.djangoproject.com/en/1.4/topics/db/aggregation/):
>>> from django.db.models import Avg
>>> Book.objects.all().aggregate(Avg('price'))