Hi,

I am trying to sum a field from a joined table and group by another
field from that joined table. Installation is the table I am querying
and CitlData is the table I am joining in.

This is the code:
installations =
Installation.objects.values('citldata__year').annotate(Sum('citldata__emissions'),Sum('citldata__allocations'))

I am getting the following error:
---------------------------------------------------------------------------
FieldError                                Traceback (most recent call
last)

/Users/pc/Documents/Work/sandbag/sandbag_svn/sandbag/sandbag/<ipython
console> in <module>()

/sw/lib/python2.6/site-packages/django/db/models/query.pyc in
values(self, *fields)
    502
    503     def values(self, *fields):
--> 504         return self._clone(klass=ValuesQuerySet, setup=True,
_fields=fields)
    505
    506     def values_list(self, *fields, **kwargs):

/sw/lib/python2.6/site-packages/django/db/models/query.pyc in
_clone(self, klass, setup, **kwargs)
    747         c.__dict__.update(kwargs)
    748         if setup and hasattr(c, '_setup_query'):
--> 749             c._setup_query()
    750         return c
    751

/sw/lib/python2.6/site-packages/django/db/models/query.pyc in
_setup_query(self)
    870         if self.extra_names is not None:
    871             self.query.set_extra_mask(self.extra_names)
--> 872         self.query.add_fields(self.field_names, False)
    873         if self.aggregate_names is not None:
    874
self.query.set_aggregate_mask(self.aggregate_names)

/sw/lib/python2.6/site-packages/django/db/models/sql/query.pyc in
add_fields(self, field_names, allow_m2m)
   1509                 self.select_fields.append(field)
   1510         except MultiJoin:
-> 1511             raise FieldError("Invalid field name: '%s'" %
name)
   1512         except FieldError:
   1513             names = opts.get_all_field_names() +
self.extra.keys() + self.aggregate_select.keys()

FieldError: Invalid field name: 'citldata__year'

However, if I filter Installation by a field from CitlData (i.e.,
installations =
Installation.objects.filter(citldata__year__exact=2009)) it works
fine. Also, if I annotate without the group by / values() function it
also works fine (i.e., installations =
Installation.objects.annotate(Sum('citldata__emissions'),Sum('citldata__allocations'))
 )

Does the values().annotate() functionality not support grouping by
joined table fields or am I missing something here?

Any help much appreciated!

Thanks,
Patrick

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to