Re: QuerySet.distinct and aggregates

2010-05-09 Thread Russell Keith-Magee
On Thu, May 6, 2010 at 7:50 PM, zinckiwi wrote: >> I was hoping for an elegant solution for when I know there's going to >> be several thousand plus instances (or hundreds of thousands after a >> year or two of deployment). So in the end, I guess I can work with it, >> but

Re: QuerySet.distinct and aggregates

2010-05-06 Thread zinckiwi
> The actual call I had tried was > Payment.objects.filter(appliedpayment__invoice__location=loc).distinct().ag > gregate(Sum('amount')) > (where amount is a field on Payment). I could do > AppliedPayment...aggregate(...), but unfortunately the AppliedPayment > splits the 'amount' into several

Re: QuerySet.distinct and aggregates

2010-05-05 Thread Wedg
The Beta name field is actually an FK to another model, like this: Payment (Alpha) <- AppliedPayment (Gamma) -> Invoice (Beta) -> Location (Name) The actual call I had tried was Payment.objects.filter(appliedpayment__invoice__location=loc).distinct().aggregate(Sum('amount')) (where amount is

Re: QuerySet.distinct and aggregates

2010-05-05 Thread zinckiwi
I'd try both and compare the processing time. What is the nature of the Beta.name equivalent in your actual model? If it is a simple field (e.g. INT) I doubt the overhead would be too bad. What might be tripping me up is having your Gamma being linked to your Alpha through an intermediate model.

Re: QuerySet.distinct and aggregates

2010-05-05 Thread Wedg
> qs = Alpha.objects.filter(gamma__beta__name='Beta').distinct()) > id_list = [x.id for x in qs] > total = > Alpha.objects.filter(id__in=id_list).aggregate(total=models.Sum('id')).get('total') I'm sure this approach would work fine, however I feel like it might be a performance issue when the

Re: QuerySet.distinct and aggregates

2010-05-05 Thread zinckiwi
> In [29]: > Alpha.objects.filter(gamma__beta__name='Beta').values('id').aggregate(model > s.Sum('id')) > Out[29]: {} > > In [30]: > Alpha.objects.filter(gamma__beta__name='Beta').values('name').aggregate(mod > els.Sum('id')) > Out[30]: {} That's odd -- I would have expected #29 to work (though

Re: QuerySet.distinct and aggregates

2010-05-04 Thread Wedg
Hrm... didn't quite work... In [18]: Alpha.objects.filter(gamma__beta__name='Beta').values('name') Out[18]: [{'name': u'Alpha'}, {'name': u'Alpha'}] In [19]: Alpha.objects.filter(gamma__beta__name='Beta').values('name').annotate(subtotal=models.Sum('id')) Out[19]: [{'subtotal': 2, 'name':

Re: QuerySet.distinct and aggregates

2010-05-04 Thread zinckiwi
> In [16]: > Alpha.objects.filter(gamma__beta__name='Beta').aggregate(models.Sum('id')) > Out[16]: {'id__sum': 2} > > In [17]: > Alpha.objects.filter(gamma__beta__name='Beta').distinct().aggregate(models. > Sum('id')) > Out[17]: {'id__sum': 2} > > As you can see, the aggregate call in 17 ignores

Re: QuerySet.distinct and aggregates

2010-05-03 Thread Wedg
Here's a real simplified version demonstrating what I was (poorly) trying to explain above: # models.py from django.db import models class Alpha( models.Model ): name = models.CharField(max_length=10, default='Alpha') def __unicode__( self ): return u'%s%s' % (self.name,

Re: QuerySet.distinct and aggregates

2010-04-30 Thread zinckiwi
Finding it a wee bit hard to follow without actual models. Could you post them? > I guess I'm writing this to confirm a behaviour and see if there might > be a work around. > > It appears that qs.aggregate(Sum('field')) ignores qs.distinct(). > > If I have something like this: > > qs =

QuerySet.distinct and aggregates

2010-04-28 Thread Wedg
I guess I'm writing this to confirm a behaviour and see if there might be a work around. It appears that qs.aggregate(Sum('field')) ignores qs.distinct(). If I have something like this: qs = Model.objects.filter(reverserelation__field=id).distinct() ... then len(qs) will return the correct