Thank You.

regards.

On 23 Mar, 23:58, Russell Keith-Magee <freakboy3...@gmail.com> wrote:
> On Tue, Mar 24, 2009 at 12:17 AM, eli <eliasz.wont...@gmail.com> wrote:
>
> > How to pass to the Sum() function (ORM) more than one field?
>
> > My problem:
>
> > class Model1(models.Model):
> >    name = models.CharField(u'Name', max_length=255)
>
> > class Model2(models.Model):
> >    fk1 = models.ForeignKey(Model1, related_name='fk_1')
> >    fk2 = models.ForeignKey(Model1, related_name='fk_2')
>
> > I want to sort data by sum of fk1 and fk2 field.
>
> > Model1.objects.annotate(myfk1=Count('fk1', distinct=True), myfk2=Count
> > ('fk2', distinct=True)).annotate(my_sum=Sum('myfk1+myfk2')).order_by('-
> > my_sum')
>
> The first two parts - the annotation of myfk1 and myfk2 is fine - but
> at present, annotation is a purely aggregate activity - you can't
> (currently) annotate an expression that is formed from two fields on
> the same model instance. This is a feature I would like to add at some
> point, but it won't be happening any time soon.
>
> You could do this using an 'extra' clause; i.e., replace the final
> annotate() with:
>
> .extra(select={'my_sum':'myfk1+myfk2'})
>
> The extra clause is used to insert raw SQL into a Django query, so you
> are responsible for choosing column names, etc However, aggregate
> column names are fairly predictable, so you shouldn't hit too many
> problems. Regardless, I strongly advise reading the documentation on
> the extra clause so that you are aware of the issues you may hit.
>
> Yours,
> Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
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