Opened 3 years ago

Closed 3 years ago

#15904 closed Cleanup/optimization (fixed)

render_comment_form executes unnecessary query for object

Reported by: stefanw Owned by: stefanw
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:


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.

The problem is in django.contrib.comments.templatetags.comments in line

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 stefanw 3 years ago.
Patch and Test

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by stefanw

Patch and Test

comment:1 Changed 3 years ago by stefanw

  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by lukeplant

  • Triage Stage changed from Unreviewed to Ready for checkin

comment:3 Changed 3 years ago by lukeplant

  • Resolution set to fixed
  • Status changed from new to closed

In [16103]:

Fixed #15904 - render_comment_form executes unnecessary query for object

Thanks to stefanw for report and patch!

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.