我测试了使用默认值渲染对象的 100 条注释{% get_comment_list %}
tag 和 django 做了 200 个评论相关的查询来列出评论+用户+个人资料,因为......
-
Comment.__unicode__
实际上打电话Comment.user
if a user_id
存在。 +1 查询
-
get_profile
+1 查询
Ouch!
我从大约 25 毫秒内的 203 个查询减少到大约 2 毫秒内的 3 个查询。
自己填充comment_list
I would 强烈建议建设comment_list
QuerySet
自己使用适当的select_related()
来电。如果经常使用,请创建一个从其他视图调用的实用函数。
def get_comments_with_user_and_profile(obj):
content_type =ContentType.objects.get_for_model(obj)
return (Comment.objects
.filter(content_type=content_type, object_pk=obj.id)
.select_related('user__profile'))
如果您希望整个框架都以这种方式运行...您必须进行猴子修补。
这不是我会轻易做的事。还有其他方法可以解决这个特定问题,但您确实“一口气”问了。
把这个放在你的某个地方INSTALLED_APPS
models.py
文件。我其实有一个monkey_patch
用于修改的应用程序django.contrib.auth.User.username
长度等(这是与这里不同的最后手段)。
from django.contrib.comments.models import Comment
from django.contrib.comments.managers import CommentManager
class CommentManager(CommentManager):
def get_query_set(self):
return (super(CommentManager, self)
.get_query_set()
.select_related('user__profile'))
Comment.add_to_class('objects', CommentManager())
有关配置文件和 select_lated() 的问题
请注意,您的UserProfile
类需要一个OneToOneField
to User
with a related_name
等于你传递给的select_related()
。在我的例子中是profile
并且你需要 django 1.2+。我记得以前偶然发现过这一点。
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
# example to use User.objects.select_related('profile')