Using .extra after using .annotate is not working
I am trying to get a field in a queryset which depends on the result
of the annotations. I tried using extra on an annotated queryset,
which doesn't seem to work.
Lets say my models looks like this:
class Foo(models.Model):
name = models.CharField(...)
class FooData(models.Model):
foo = models.ForeignKey(Foo)
sales = models.PositiveIntegerField()
items_sold = models.PositiveIntegerField()
I have a queryset of Foo objects
some_foos = Foo.objects.filter()
I have annotated it to get the sum on FooData.
some_foos.object.annotate(
total_sales=Sum("foodata__sales"),
total_items_sold=Sum("foodata__items_sold")
)
Now I want to get the average price for which I am trying to
use .extra. They don't seem to work. (I have looked the generated sql
queries).
#Wont work, fields not defined yet.
some_foos.object.annotate(
total_sales=Sum("foodata__sales"),
total_items_sold=Sum("foodata__items_sold")
).extra(select={'price_avg': "total_sales/total_items_sold"})
#Wont work, this adds an extra clause in group by
price_avg = "SUM(appname_foodata.sales)/
SUM(appname_foodata.items_sold)"
some_foos.object.annotate(
total_sales=Sum("foodata__sales"),
total_items_sold=Sum("foodata__items_sold")
)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home