Re: Aggregation across foreign keys / complex aggregation and annotation

2009-01-16 Thread Alex Koshelev
Try this:

Process.objects.filter(exchange__input__unit__unit="kilogram")\
 .annotate(sum_amount=Sum("exchange__amount"))\
 .filter(sum_amount__gt=1)


On Fri, Jan 16, 2009 at 12:38 PM, Christopher Mutel <cmu...@gmail.com>
wrote:
>
> Following the general rule that no good deed goes unpunished, I have a
> question about complex aggregation / annotation or annotation across
> foreign keys.
>
> I have the following simplified model:
>
> class Unit(models.Model):
>unit = models.TextField()
>
> class Process(models.Model):
>process = models.TextField()
>unit = models.ForeignKey(Unit)
>
> class Exchange(models.Model):
>input = models.ForeignKey(Process)
>output = models.ForeignKey(Process)
>amount = models.FloatField()
>
> Is there a way for me to get a queryset for Process outputs which have
> a sum of inputs greater than one kilogram?
>
> You can do this (painfully) now by looping:
>
> foo = []
> for p in Process.objects.all():
>if sum(Exchange.objects.filter(ouput=p,
> input__unit__unit="kilogram").values_list('amount', flat=True)) > 1:
>foo.append(p)
>
> Sorry if this is a basic question, but I have been trying to figure it
> out for a while, and without success.
>
> -Chris
>
> >
>

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



Aggregation across foreign keys / complex aggregation and annotation

2009-01-16 Thread Christopher Mutel

Following the general rule that no good deed goes unpunished, I have a
question about complex aggregation / annotation or annotation across
foreign keys.

I have the following simplified model:

class Unit(models.Model):
unit = models.TextField()

class Process(models.Model):
process = models.TextField()
unit = models.ForeignKey(Unit)

class Exchange(models.Model):
input = models.ForeignKey(Process)
output = models.ForeignKey(Process)
amount = models.FloatField()

Is there a way for me to get a queryset for Process outputs which have
a sum of inputs greater than one kilogram?

You can do this (painfully) now by looping:

foo = []
for p in Process.objects.all():
if sum(Exchange.objects.filter(ouput=p,
input__unit__unit="kilogram").values_list('amount', flat=True)) > 1:
foo.append(p)

Sorry if this is a basic question, but I have been trying to figure it
out for a while, and without success.

-Chris

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