Permalink
Browse files

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
  • Loading branch information...
1 parent d8bfd39 commit 1b6670dd590ef8f235166ce966d28ac49f322c54 @spookylukey spookylukey committed Apr 26, 2011
@@ -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 ''
@@ -40,6 +40,11 @@ def testRenderCommentFormFromLiteral(self):
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 }}"

0 comments on commit 1b6670d

Please sign in to comment.