Django

Code

Ticket #2351 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

MySQL syntax error on object.all().count(), when called from template

Reported by: gumuz Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

I have a blogpost model which is referenced by a Comment class. Thus, the Blogpost model has a comment_set attribute.

I gave the Blogpost model a method like this:

def comments(self):
    return self.comment_set.all()

When i access it from the shell and use the .count() method on the QuerySet, everything is fine:

>>> from devlog.weblog.models import Post
>>> p = Post.objects.all()[4]
>>> p.comments()
[<Comment: gumuz - 2006-07-12 07:10:30>, <Comment: gumuz - 2006-07-12 07:09:56>]
>>> p.comments().count()
2L

When i do the same from my template:

{{ object.comments.count }}

I get the following error:

Exception Type:  	ProgrammingError
Exception Value: 	(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'count,1' at line 1")
Exception Location: 	/usr/lib/python2.4/site-packages/MySQLdb/connections.py in defaulterrorhandler, line 33

The length-filter works fine though:

{{ object.comments|length }}

Attachments

diff_fix_queryset_getitem.txt (0.7 kB) - added by marcin@elksoft.pl on 02/18/07 12:35:20.
backtrace_short.txt (1.4 kB) - added by marcin@elksoft.pl on 02/19/07 15:29:11.
backtrace_detailed.txt (12.9 kB) - added by marcin@elksoft.pl on 02/19/07 15:32:01.

Change History

09/12/06 23:22:35 changed by racter@gmail.com

this error only happens for me when nesting -- ie if i try:

{% for object in category.object_set.all %}
{{ object.subobject_set.count }}
{% endfor %}

i get the same error.

02/03/07 20:42:39 changed by Jonas

I get the same error. count fails from template but |length works fine.

02/04/07 02:14:16 changed by Michael Radziej <mir@noris.de>

Please, send the full bracktrace.

02/18/07 12:35:20 changed by marcin@elksoft.pl

  • attachment diff_fix_queryset_getitem.txt added.

02/18/07 12:40:18 changed by marcin@elksoft.pl

  • has_patch set to 1.

I just ran into the same problem with Postgresql.

The cause is a bug in QuerySet?.getitem: it assumes that the parameter it gets is either a slice or an integer, but does not really check the latter. When you put {{ somequeryset.qwe }} in your template and template.resolve_variable tries to resolve it using a dictionary lookup, QuerySet?.getitem treats qwe like an integer index, which results in queries ending with "limit 1 offset qwe".

The attached patch fixes the problem.

02/19/07 10:47:02 changed by mir@noris.de

It would be rather nice if someone posts the full backtrace. I personally refuse to triage a bug report without the full information, and it's rather tedious to reconstruct the problem when you're dealing with a lot of tickets.

02/19/07 15:29:11 changed by marcin@elksoft.pl

  • attachment backtrace_short.txt added.

02/19/07 15:32:01 changed by marcin@elksoft.pl

  • attachment backtrace_detailed.txt added.

02/19/07 15:38:25 changed by marcin@elksoft.pl

mir: here you are. Note the resulting SQL query at the end.

All the interesting parts of code:

models.py:

    class Article(models.Model):
        text = models.TextField()

urls.py:

    urlpatterns = patterns('',
        (r'^$', 'django.views.generic.simple.direct_to_template',
         {'extra_context': {'object_list': Article.objects.all()},
          'template': 'index.html'}),
    )

index.html:

{{ object_list.count }}

I tried to attach the detailed backtrace in HTML, but Akismet said it was spam.

02/22/07 21:45:49 changed by SmileyChris

  • needs_tests set to 1.
  • stage changed from Unreviewed to Accepted.

I'll accept. Probably could do with some tests showing the failure.

02/23/07 10:30:12 changed by Noah Slater <nslater@gmail.com>

I can confirm identical behaviour.

03/14/07 22:57:28 changed by ubernostrum

#2945 was a duplicate of this one.

03/14/07 22:58:26 changed by ubernostrum

And for the record, #2945 has a full traceback of this bug. And I've seen it happen on SQLite as well.

03/21/07 22:00:12 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [4772]) Fixed #2351 -- Fixed problem with using ".count" attribute of QuerySets? in templates.


Add/Change #2351 (MySQL syntax error on object.all().count(), when called from template)




Change Properties
Action