On Tue, Jun 23, 2009 at 7:31 AM, Russell Keith-Magee <freakboy3...@gmail.com
> wrote:

>
> On Tue, Jun 23, 2009 at 2:36 AM, pr<crico...@gmail.com> wrote:
> >
> > Hi,
> >
> > I want to get total price (quantity*price) in this example for objest
> > list (not one) of A class
> >
> > class A(models.Model)
> >  name = models.CharField(max_length=255)
> >
> > class B(models.Model)
> >  quantity = models.PositiveIntegerField()
> >  price = models.DecimalField(max_digits=10, decimal_places=2)
> >  a = models.ForeignKey(A)
> >
> > I try:
> > A.objects.all().select_related().annotate(my_sum=Sum
> > ('b__price*b__quantity'))
> > but it doesn't work...
>
> You can't currently annotate a computed expression onto a model (at
> least, not using the annotate() clause). You can only annotate an
> aggregate, which must be a Sum/max/min etc of a single field.
>
> For the moment, if you want to do something like this, you will need
> to look into using extra(select=...), or using custom SQL.
>
> Annotating computed fields is something that I would like to add in
> the future, but probably it probably won't get done until at least
> Django v1.2, probably later (unless someone else volunteers to do the
> work).
>
> Yours,
> Russ Magee %-)
>
> >
>
It's actually possible if you write you're own custom aggregate (and I feel
confident that writing an aggregate that could work with F() objects
wouldn't be overly difficult, and even if it is writing one that ahrd codes
your expression would be near trivial).

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
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