Opened 6 years ago

Closed 6 years ago

#15904 closed Cleanup/optimization (fixed)

render_comment_form executes unnecessary query for object

Reported by: Stefan Wehrmeyer Owned by: Stefan Wehrmeyer
Component: contrib.comments Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX:

Description

The template tag render_comment_form when given an object like this:

{% render_comment_form for obj %}

will retrieve obj from the database via content type and pk even though it is already present in the rendering context.

Details:
The problem is in django.contrib.comments.templatetags.comments in line http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/templatetags/comments.py#L127

The context already contains the object, but get_form ignores that and hits the database via get_object_for_this_type. This is unnecessary and especially bad when you render multiple comment forms for different objects on one page.

I attached a patch that also includes a simple test.

Attachments (1)

render_comments_form_query_count.diff (2.1 KB) - added by Stefan Wehrmeyer 6 years ago.
Patch and Test

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by Stefan Wehrmeyer

Patch and Test

comment:1 Changed 6 years ago by Stefan Wehrmeyer

Easy pickings: set

comment:2 Changed 6 years ago by Luke Plant

Triage Stage: UnreviewedReady for checkin

comment:3 Changed 6 years ago by Luke Plant

Resolution: fixed
Status: newclosed

In [16103]:

Fixed #15904 - render_comment_form executes unnecessary query for object

Thanks to stefanw for report and patch!

Note: See TracTickets for help on using tickets.
Back to Top