Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Sorting objects according to a field from a foreign field [RSS Feed]

#1 June 22, 2010 15:35:04

Benedict V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Sorting objects according to a field from a foreign field


I have an issue sorting objects.
First, let me explain the models i'm using:

class Call(models.Model):
...
priority = models.ForeignKey(Priority)

class Priority(models.Model):
title=models.CharField(max_length=30)
description=models.CharField(max_length=255)
weight=models.IntegerField()


I want to sort the calls according to their priority and more exactly the
weight assigned
to priority.

I haven't found a way to do that.
I would need something like this:
c=Call.objects.all().order_by('-priority.weight')

Or can i specify the way the Priority class should be sorted?

Thanks,
Benedict

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 22, 2010 15:42:24

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Sorting objects according to a field from a foreign field


On Jun 22, 3:34 pm, Benedict Verheyen <benedict.verhe...@gmail.com>
wrote:
> I have an issue sorting objects.
> First, let me explain the models i'm using:
>
> class Call(models.Model):
>     ...
>     priority = models.ForeignKey(Priority)
>
> class Priority(models.Model):
>     title=models.CharField(max_length=30)
>     description=models.CharField(max_length=255)
>     weight=models.IntegerField()
>
> I want to sort the calls according to their priority and more exactly the
> weight assigned
> to priority.
>
> I haven't found a way to do that.
> I would need something like this:
>   c=Call.objects.all().order_by('-priority.weight')
>
> Or can i specify the way the Priority class should be sorted?
>
> Thanks,
> Benedict

As described in the documentation , you use the double-underscore
syntax for sorting across relationships.
Call.objects.all().order_by('-priority__weight')

:http://docs.djangoproject.com/en/1.2/ref/models/querysets/#order-by-fields
--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 22, 2010 15:48:58

Benedict V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Sorting objects according to a field from a foreign field


On 22/06/2010 16:42, Daniel Roseman wrote:
>
> As described in the documentation , you use the double-underscore
> syntax for sorting across relationships.
> Call.objects.all().order_by('-priority__weight')
>
> :http://docs.djangoproject.com/en/1.2/ref/models/querysets/#order-> by-fields
> --
> DR.
>

Thanks for the quick response.
Hhhm, i searched the docs and didn't see it.

Anyway, i'll try your solution.

In the mean time i found another solution:

- Change the model by adding a default ordering via the Meta class

class Priority:
class Meta:
ordering = ('weight',)

Then this works:
Call.objects.all().order_by('-priority')

Regards,
Benedict


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 24, 2010 08:30:00

d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Sorting objects according to a field from a foreign field


On Jun 22, 4:48 pm, Benedict Verheyen <benedict.verhe...@gmail.com>
wrote:
> On 22/06/2010 16:42, Daniel Roseman wrote:
>
>
>
> > As described in the documentation , you use the double-underscore
> > syntax for sorting across relationships.
> >     Call.objects.all().order_by('-priority__weight')
>
> > :http://docs.djangoproject.com/en/1.2/ref/models/querysets/#order-> > by-fields
> > --
> > DR.
>
> Thanks for the quick response.
> Hhhm, i searched the docs and didn't see it.

Quote: "To order by a field in a different model, use the same syntax
as when you are querying across model relations. That is, the name of
the field, followed by a double underscore (__), followed by the name
of the field in the new model, and so on for as many models as you
want to join." (5th para under the "order_by" section)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Sorting objects according to a field from a foreign field [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support