I'm trying to understand how to use aggregation with "computed
columns", that is, columns that are expressions defined in the extra()
method, but it seems these aren't available in downstream parts of the
query:

class Trans(models.Model):
    parent = models.ForeignKey('self', null=True, blank=True)
    account = models.ForeignKey(Account, null=True, blank=True)
    date = models.DateField(null=True, blank=True)
    description = models.TextField(blank=True)
    memo = models.TextField(blank=True)
    quantity = models.FloatField(blank=True)
    price = models.FloatField(blank=True)
    class Meta:
        db_table = u'trans'

>>> t = 
>>> Trans.objects.extra(select={'amount':'quantity*price'}).aggregate(total=Sum('amount'))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/django/db/models/query.py",
line 277, in aggregate
    is_summary=True)
  File "/Library/Python/2.6/site-packages/django/db/models/sql/
query.py", line 1471, in add_aggregate
    field_list, opts, self.get_initial_alias(), False)
  File "/Library/Python/2.6/site-packages/django/db/models/sql/
query.py", line 1737, in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'amount' into field. Choices are:
account, date, description, id, memo, parent, price, quantity, trans
>>>

Am I going about this wrong? This is how I would do it in SQL:

select sum(quantity * price) as total from trans;

Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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