Re: Vanilla Install problem with viewing profile [Figured out the solution - fix described]

2 views
Skip to first unread message

Eric Peters

unread,
Aug 11, 2009, 9:07:22 PM8/11/09
to cnp...@googlegroups.com
OK, well it may not be THE solution, but its A solution

in views.py:

    awards = Award.objects.extra(
        select={'id': 'badge.id', 'count': 'count(badge_id)', 'name':'badge.name', 'description': 'badge.description', 'type': 'badge.type'},
        tables=['award', 'badge'],
        order_by=['-awarded_at'],
        where=['user_id=%s AND badge_id=badge.id'],
        params=[user.id]
    ).values('id', 'count', 'name', 'description', 'type')
    total_awards = awards.count()
    awards.query.group_by = ['badge_id']

becomes =>


    awards = Award.objects.extra(
        select={'id': 'badge.id', 'name':'badge.name', 'description': 'badge.description', 'type': 'badge.type'},
        tables=['award', 'badge'],
        order_by=['-awarded_at'],
        where=['user_id=%s AND badge_id=badge.id'],
        params=[user.id]
    ).values('id', 'count', 'name', 'description', 'type')
    total_awards = awards.count()
    awards.query.group_by = ['badge_id']

I ripped out the count column and everything seems to work fine.


On Thu, Jul 30, 2009 at 8:44 AM, Eric Peters <ericp...@gmail.com> wrote:
So signup/sign in/posting/voting/replying and etc all works but once I click on a profile ID (like) - http://localhost:8000/users/1/Eric/ - I trigger an error that has to do with the  121 :                          {% for award in awards %} 

(That current table is blank and there aren't any SQL that populates it in the awards table)  I tried adding an award but wasn't sure what hte "object id" should be in the nimda interface.

Environment:

Request Method: GET
Request URL: http://localhost:8000/users/1/Eric/
Django Version: 1.1
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.humanize',
 'forum',
 'django_authopenid',
 'debug_toolbar']
Installed Middleware:
('django.middleware.gzip.GZipMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.transaction.TransactionMiddleware',
 'middleware.pagesize.QuestionsPageSizeMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Template error:
In template /Users/eric/Work/answers/templates/user_stats.html, error at line 121
   Caught an exception while rendering: (1111, 'Invalid use of group function')
   111 :                 </td>


   112 :                 <td >


   113 :                     <h2>{% trans "Badges" %}</h2>


   114 :                 </td>


   115 :             </tr>


   116 :         </table>


   117 :         <div class="user-stats-table">


   118 :             <table>


   119 :                 <tr>


   120 :                     <td width="180" style="line-height:35px">


   121 :                          {% for award in awards %} 


   122 :                             <a href="/badges/{{award.id}}/{{award.name}}" title="{{ award.description }}" class="medal"><span class="badge{{ award.type }}">&#9679;</span>&nbsp;{{ award.name }}</a><span class="tag-number"> × {{ award.count|intcomma }}</span><br>


   123 :                             {% if forloop.counter|divisibleby:"6" %}


   124 :                                 </td>


   125 :                                 <td width="180" style="line-height:35px">


   126 :                             {% endif %}


   127 :                         {% endfor %}


   128 :                     </td>


   129 :                 </tr>


   130 :             </table>


   131 :         </div>


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/Users/eric/Work/answers/forum/views.py" in user
  898.     return func(request, id, user_view)
File "/Users/eric/Work/answers/forum/views.py" in user_stats
  1024.     }, context_instance=RequestContext(request))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/loader.py" in render_to_string
  108.     return t.render(context_instance)
File "/Users/eric/python/lib/debug_toolbar/stats.py" in wrapped
  101.             return _stats.collection.run(func, key, *args, **kwargs)
File "/Users/eric/python/lib/debug_toolbar/stats.py" in run
  18.         value = func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  178.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  779.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  97.         return compiled_parent.render(context)
File "/Users/eric/python/lib/debug_toolbar/stats.py" in wrapped
  101.             return _stats.collection.run(func, key, *args, **kwargs)
File "/Users/eric/python/lib/debug_toolbar/stats.py" in run
  18.         value = func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  178.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  779.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  97.         return compiled_parent.render(context)
File "/Users/eric/python/lib/debug_toolbar/stats.py" in wrapped
  101.             return _stats.collection.run(func, key, *args, **kwargs)
File "/Users/eric/python/lib/debug_toolbar/stats.py" in run
  18.         value = func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  178.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  779.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  24.         result = self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  779.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/loader_tags.py" in render
  24.         result = self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py" in render
  779.                 bits.append(self.render_node(node, context))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/debug.py" in render_node
  81.             raise wrapped

Exception Type: TemplateSyntaxError at /users/1/Eric/
Exception Value: Caught an exception while rendering: (1111, 'Invalid use of group function')


Eric Peters

unread,
Aug 11, 2009, 9:53:41 PM8/11/09
to cnp...@googlegroups.com
OK, last one for today, I swear!  

It turns out the "hack" for group by was disabled for the aggregation functionality in django 1.1 - so the code would need to be migrated to that to work for 1.1 - everything else seems to work flawlessly for django 


I've reverted back to django 1.0 and its working great.

-Eric

wezer wezerism

unread,
Aug 12, 2009, 7:54:25 AM8/12/09
to CNProg open discussion
Eric,

Had the same problem with django 1.1. Now using django 1.0.3 does not
get this issue...



On Aug 12, 4:53 am, Eric Peters <ericpet...@gmail.com> wrote:
> OK, last one for today, I swear!
> It turns out the "hack" for group by was disabled for the aggregation
> functionality in django 1.1 - so the code would need to be migrated to that
> to work for 1.1 - everything else seems to work flawlessly for django
>
> (My reference:http://stackoverflow.com/questions/327807/django-equivalent-for-count...
> )
>
> I've reverted back to django 1.0 and its working great.
>
> -Eric
>
> > On Thu, Jul 30, 2009 at 8:44 AM, Eric Peters <ericpet...@gmail.com> wrote:
>
> >> So signup/sign in/posting/voting/replying and etc all works but once I click on a profile ID (like) -http://localhost:8000/users/1/Eric/- I trigger an error that has to do with the  121 :                          {% for award in awards %}

Evgeny

unread,
Aug 12, 2009, 10:52:57 AM8/12/09
to CNProg open discussion
Hi Eric,

could you confirm whether your solution works correctly for all
versions?
is it possible to find one that does?
Cheers!

Evgeny.

On Aug 11, 6:53 pm, Eric Peters <ericpet...@gmail.com> wrote:
> OK, last one for today, I swear!
> It turns out the "hack" for group by was disabled for the aggregation
> functionality in django 1.1 - so the code would need to be migrated to that
> to work for 1.1 - everything else seems to work flawlessly for django
>
> (My reference:http://stackoverflow.com/questions/327807/django-equivalent-for-count...
> )
>
> I've reverted back to django 1.0 and its working great.
>
> -Eric
>
> > On Thu, Jul 30, 2009 at 8:44 AM, Eric Peters <ericpet...@gmail.com> wrote:
>
> >> So signup/sign in/posting/voting/replying and etc all works but once I click on a profile ID (like) -http://localhost:8000/users/1/Eric/- I trigger an error that has to do with the  121 :                          {% for award in awards %}

Eric Peters

unread,
Aug 12, 2009, 12:29:52 PM8/12/09
to cnp...@googlegroups.com
Based upon the research I've done so far, it DOES NOT look to be a 1.0 + 1.1 compatible solution.  Technically the .group_by was suppose to be an internal method only in django 1.0

It looks like you end up having to do a big if/then/else statement (sort of)  I'll see if I can hack together a real patch for it but here's a patch for a completely different project - you can see how they do the if django 1.1 do blah else do bleh

Index: managers.py
===================================================================
--- managers.py (revision 69)
+++ managers.py (working copy)
@@ -31,14 +31,25 @@
             return {}
 
         ctype = ContentType.objects.get_for_model(objects[0])
-        ctype = ContentType.objects.get_for_model(objects[0])
-        queryset = self.filter(
-            object_id__in=object_ids, content_type=ctype).extra(
-            select={
-                'score': 'COALESCE(SUM(vote), 0)',
-                'num_votes': 'COALESCE(COUNT(vote), 0)',
-        }).values_list('object_id', 'score', 'num_votes')
-        queryset.query.group_by.append('object_id')
+        if hasattr(models, 'Sum'): # django 1.1
+            queryset=self.filter(
+                object_id__in=object_ids,
+                content_type=ctype).values(
+                'object_id').annotate(
+                score=models.Sum('vote'),
+                num_votes=models.Count('vote'))
+            return dict((d['object_id'],
+                         {'score' : d['score'],
+                          'num_votes' : d['num_votes']}) for d in queryset)
+        else:
+            queryset = self.filter(
+                object_id__in=object_ids, content_type=ctype).extra(
+                select={
+                    'score': 'COALESCE(SUM(vote), 0)',
+                    'num_votes': 'COALESCE(COUNT(vote), 0)',
+                    }).values_list('object_id', 'score', 'num_votes')
+
+            queryset.query.group_by.append('object_id')
         vote_dict = {}
         for id, score, num_votes in queryset:
             vote_dict[id] = {'score': int(score), 'num_votes': int(num_votes)}

Evgeny

unread,
Aug 12, 2009, 12:38:58 PM8/12/09
to CNProg open discussion
maybe you could set up a direct sql call - not sure...
> ...
>
> read more »

Mike Chen

unread,
Aug 13, 2009, 12:05:31 PM8/13/09
to cnp...@googlegroups.com
I've made an update for fixing this temporarily.

I only test it on Django 1.1 and please let me know if you have problems on other versions.

Regards,
-Mike

Adolfo Fitoria

unread,
Aug 16, 2009, 8:49:17 PM8/16/09
to cnp...@googlegroups.com
Mike.

I recently applied your fix and I get this error: cannot import name Count.
The debug info tells that the error is in forum/views.py on line 1552
(from django.db.models import Count) complete debug at:
http://gist.github.com/168831


I am using Django 1.0.2 final.

--
Adolfo Fitoria
http://fitoria.net

Adolfo Fitoria

unread,
Aug 16, 2009, 10:25:40 PM8/16/09
to cnp...@googlegroups.com
I fixed the error check my commit in my fork[1] (commit link not
available at the moment due to errors on the site)

Check the attachment for the patch

[1]: http://github.com/fitoria/CNPROG/tree/master

0078-fixing-error-on-import-in-forum-views-to-work-with-d.patch

Evgeny

unread,
Aug 21, 2009, 2:51:29 PM8/21/09
to CNProg open discussion
maybe we should have something that checking object attributes with

hasattr()

and then have branched code depending on the result of checks
instead of using DJANGO_VERSION?

Evgeny.
>  0078-fixing-error-on-import-in-forum-views-to-work-with-d.patch
> 1KViewDownload
Reply all
Reply to author
Forward
0 new messages