Fixed #15904 - render_comment_form executes unnecessary query for object

Thanks to stefanw for report and patch!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16103 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2011-04-26 11:28:50 +00:00
parent d8bfd3955d
commit 1b6670dd59
2 changed files with 19 additions and 3 deletions

View File

@ -122,12 +122,23 @@ class CommentFormNode(BaseCommentNode):
"""Insert a form for the comment model into the context."""
def get_form(self, context):
ctype, object_pk = self.get_target_ctype_pk(context)
if object_pk:
return comments.get_form()(ctype.get_object_for_this_type(pk=object_pk))
obj = self.get_object(context)
if obj:
return comments.get_form()(obj)
else:
return None
def get_object(self, context):
if self.object_expr:
try:
return self.object_expr.resolve(context)
except template.VariableDoesNotExist:
return None
else:
object_pk = self.object_pk_expr.resolve(context,
ignore_failures=True)
return self.ctype.get_object_for_this_type(pk=object_pk)
def render(self, context):
context[self.as_varname] = self.get_form(context)
return ''

View File

@ -40,6 +40,11 @@ class CommentTemplateTagTests(CommentTestCase):
def testRenderCommentFormFromObject(self):
self.testRenderCommentForm("{% render_comment_form for a %}")
def testRenderCommentFormFromObjectWithQueryCount(self):
def test():
self.testRenderCommentFormFromObject()
self.assertNumQueries(1, test)
def testGetCommentCount(self, tag=None):
self.createSomeComments()
t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"