Ticket #15904: render_comments_form_query_count.diff

File render_comments_form_query_count.diff, 2.1 KB (added by Stefan Wehrmeyer, 14 years ago)

Patch and Test

  • django/contrib/comments/templatetags/comments.py

    diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
    index 42691c6..8bc61cd 100644
    a b class CommentFormNode(BaseCommentNode):  
    122122    """Insert a form for the comment model into the context."""
    123123
    124124    def get_form(self, context):
    125         ctype, object_pk = self.get_target_ctype_pk(context)
    126         if object_pk:
    127             return comments.get_form()(ctype.get_object_for_this_type(pk=object_pk))
     125        obj = self.get_object(context)
     126        if obj:
     127            return comments.get_form()(obj)
    128128        else:
    129129            return None
    130130
     131    def get_object(self, context):
     132        if self.object_expr:
     133            try:
     134                return self.object_expr.resolve(context)
     135            except template.VariableDoesNotExist:
     136                return None
     137        else:
     138            object_pk = self.object_pk_expr.resolve(context,
     139                    ignore_failures=True)
     140            return self.ctype.get_object_for_this_type(pk=object_pk)
     141
    131142    def render(self, context):
    132143        context[self.as_varname] = self.get_form(context)
    133144        return ''
  • tests/regressiontests/comment_tests/tests/templatetag_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
    index 4c90d9d..0ead6c2 100644
    a b class CommentTemplateTagTests(CommentTestCase):  
    4040    def testRenderCommentFormFromObject(self):
    4141        self.testRenderCommentForm("{% render_comment_form for a %}")
    4242
     43    def testRenderCommentFormFromObjectWithQueryCount(self):
     44        def test():
     45            self.testRenderCommentFormFromObject()
     46        self.assertNumQueries(1, test)
     47
    4348    def testGetCommentCount(self, tag=None):
    4449        self.createSomeComments()
    4550        t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
Back to Top