Opened 9 years ago

Closed 9 years ago

#24387 closed Bug (invalid)

Django admin, exception in queryset when using values() before annotate

Reported by: essadek Owned by: nobody
Component: contrib.admin Version: 1.6
Severity: Normal Keywords: Django Admin, aggregation, change list
Cc: Simon Charette Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class AppModel(models.Model):

    nbr = models.BigIntegerField(blank=True, null=True)
    region = models.ForeignKey(AppWilaya,blank=True, null=True)
    date_preorder = models.DateField(blank=True, null=True)
    id = models.IntegerField(primary_key=True,blank=True, db_column='dummy_id')
class AppModelAdmin(admin.ModelAdmin):
....
def queryset(self, request):
        qs = super(AppModelAdmin, self).get_queryset(request)  
        qs=qs.values("region").annotate(total=Sum( 'nbr'))

Exception Value: 'dict' object has no attribute '_meta'
Exception Location: [PATH_TO]\lib\site-packages\django\contrib\admin\util.py in lookup_field, line 242

Note when removing .values("region") the exception doesn't occur.

Change History (4)

comment:1 by Simon Charette, 9 years ago

Cc: Simon Charette added
Resolution: needsinfo
Status: newclosed

It's not clear to me what you're trying to accomplish here, the admin requires a normal queryset not a value one.

Are you trying to select only the region field? In this case you should use only and not values.

qs = qs.annotate(total=Sum('nbr')).only('region')

comment:2 by essadek, 9 years ago

What is needed is aggregation, means sum(nbr )group by region:

SELECT region, \
          SUM(nbr) AS  total\
          FROM  app_model\
          GROUP BY  region\

Never seen usage of "only()" In all search I've done on aggregation in Django Admin.

comment:3 by essadek, 9 years ago

Resolution: needsinfo
Status: closednew

comment:4 by Tim Graham, 9 years ago

Resolution: invalid
Status: newclosed

Please see TicketClosingReasons/UseSupportChannels for other ways to get help with constructing proper queries besides reading the documentation. The information you've provided so far suggests problems in your own code, not in Django itself.

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