Can I sort a related model reference in a template?

3 views
Skip to first unread message

Micah Carrick

unread,
Mar 12, 2011, 8:09:33 AM3/12/11
to django...@googlegroups.com
If I have a Product model which has a one-to-many relationship with ProductImage model...

I can get the images in the template using product.productimage_set.all, however, is there any way to sort that or will I instead have to query the product images in the view?

--
    Green Tackle - Environmentally Friendly Fishing Tackle
    www.GreenTackle.com

     Email: mi...@greentackle.com
     Phone: 971.270.2206
     Toll Free: 877.580.9165
     Fax: 503.946.3106



Shawn Milochik

unread,
Mar 12, 2011, 8:27:06 AM3/12/11
to django...@googlegroups.com

You can put a sort order in the model's meta info. That way you'll always have a default sort.

Micah Carrick

unread,
Mar 12, 2011, 8:50:14 AM3/12/11
to django...@googlegroups.com
Yeash. Don't know how I missed that. Thanks!

On Sat, Mar 12, 2011 at 5:27 AM, Shawn Milochik <sh...@milochik.com> wrote:

You can put a sort order in the model's meta info. That way you'll always have a default sort.

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





Tom Evans

unread,
Mar 14, 2011, 7:49:37 AM3/14/11
to django...@googlegroups.com, Shawn Milochik
On Sat, Mar 12, 2011 at 1:27 PM, Shawn Milochik <sh...@milochik.com> wrote:
> You can put a sort order in the model's meta info. That way you'll always
> have a default sort.
>

If you do this, every query you execute returning that model will be
sorted. This can lead to serious performance issues if sorting is only
required in certain scenarios - sorting is never free.

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages