如何解决类似| select_related的函数在RawQuerySet中不起作用
| 我在Django 1.3中有原始SQL查询的问题。当我从ORM使用select_related()时,可以在一个查询中获得全部所需数据。activities = UserActivity.objects.filter(Q(user__in=friends_list),Q(activity_type = UserActivity.TYPE_POST,post__status=Post.PUBLIC_STATUS)|Q(activity_type = UserActivity.TYPE_SHORT_REVIEW)|Q(activity_type = UserActivity.TYPE_COMMENT)).select_related()
上面的例子给了我与数据库的一种连接。
[{\'time\': \'0.164\',\'sql\': \'SELECT \"useractivity_useractivity\".\"id\",\"useractivity_useractivity\".\"activity_type\",\"useractivity_useractivity\".\"status\",\"useractivity_useractivity\".\"permalink\",\"useractivity_useractivity\".\"user_id\",\"useractivity_useractivity\".\"username\",\"useractivity_useractivity\".\"object_id\",\"useractivity_useractivity\".\"watching_object_id\",\"useractivity_useractivity\".\"post_id\",\"useractivity_useractivity\".\"short_review_id\",\"useractivity_useractivity\".\"comment_id\",\"useractivity_useractivity\".\"link_id\",\"useractivity_useractivity\".\"film_id\",\"useractivity_useractivity\".\"checkin_id\",\"useractivity_useractivity\".\"title\",\"useractivity_useractivity\".\"content\",\"useractivity_useractivity\".\"film_title\",\"useractivity_useractivity\".\"film_permalink\",\"useractivity_useractivity\".\"url\",\"useractivity_useractivity\".\"url_kind\",\"useractivity_useractivity\".\"video_thumb\",\"useractivity_useractivity\".\"spoilers\",\"useractivity_useractivity\".\"is_first_post\",\"useractivity_useractivity\".\"number_of_comments\",\"useractivity_useractivity\".\"channel_name\",\"useractivity_useractivity\".\"channel_id\",\"useractivity_useractivity\".\"checkin_date\",\"useractivity_useractivity\".\"rating\",\"useractivity_useractivity\".\"featured\",\"useractivity_useractivity\".\"modified_at\",\"useractivity_useractivity\".\"created_at\",\"useractivity_useractivity\".\"LANG\",\"useractivity_useractivity\".\"is_sent\",\"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 \"useractivity_useractivity\" INNER JOIN \"auth_user\" ON (\"useractivity_useractivity\".\"user_id\" = \"auth_user\".\"id\") LEFT OUTER JOIN \"blog_post\" ON (\"useractivity_useractivity\".\"post_id\" = \"blog_post\".\"parent_id\") LEFT OUTER JOIN \"core_object\" ON (\"blog_post\".\"parent_id\" = \"core_object\".\"id\") WHERE (\"useractivity_useractivity\".\"LANG\" = E\\\'en\\\' AND \"useractivity_useractivity\".\"user_id\" IN (10,16,12,14,6,32,13,40,4,15,47,5,50,51,53,33,24,22,153,208,207,65,23,398,203,312,582,402,416,30,144,979,173,133,690,1748,38,2104,1918,1989,1850,1788,2389,395,2535,2658,2287,953,2845,2790,2849,3016,2656,3114,3190,156,2981,3193,2884,193,445,3742,1069,4035,4178,2568,1409,2292,3561,4801,162,4968,4090,4916,4548,5213,2878,5208,3120,4294,5406,5944,6221,6266,6305,3559,3577,2846,2580,2416,970,6969,7126,7175,6401,5237,172,792,17,7246,1779,134,6433,3725,6343,2121,4193,721,958,375,2875,6124,754,137,241,219,540,337,152,955,6717,6808,7121,2752,6802,6793,5146,4183,7349,7375,6452,284,6544,4276,7377,7387,151,7314,7357,1373,7397,6260,500,7437,4961,7478,4559,7545,7554,7555,7488,7092,4615,7285,7619,7621,7344,6409,422,7686) AND ((\"core_object\".\"status\" = 1 AND \"useractivity_useractivity\".\"activity_type\" = 1 ) OR \"useractivity_useractivity\".\"activity_type\" = 2 OR \"useractivity_useractivity\".\"activity_type\" = 3 ))\'}]
但是,当我尝试将相同的查询放入raw中时:
activity = UserActivity.objects.raw(query)
list(activity)
我对数据库有数千个查询,例如:
SELECT \"auth_user\".\"id\",\"auth_user\".\"date_joined\" FROM \"auth_user\" WHERE \"auth_user\".\"id\" = 4801
看起来RawQuerySet不会一次执行获取所有数据,但是每次调用friends_list时,都会查询数据库有关用户权限的信息。
尽管执行相同的查询,为什么RawQuerySet不能像select_related一样工作?
解决方法
好。问题不在我的查询中。它已连接到打印结果。函数尝试打印用户而不是用户名,因此必须从数据库中获取每个用户。
是
def __unicode__(self):
return \"%s\" % self.user
应该
def __unicode__(self):
return self.username
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。