Hello everyone,


A (long) question:

I have two models, linked by a single key. LinkClass ("search engines", 
...) and Link ("http://www.google.com";, "http://www.yahoo.com";)

Now, in Python I can do:

for link_class in LinkClass.objects.all():
     print link_class.name
     for link in link_class.link_set.all().order_by("name"):
             print "\t", link

In the templates I do:

{% for link_class in object_list %}
     <h2>{{ link_class.name }}</h2>
     <ul>
         {% for link in link_class.objects.all %}
         <li>{{ link.name }}
         {% endfor %}
     </ul>
{% endfor %}

But I can't "order by" in the link_class.objects.all (or I missed some 
functionality)!

Now there's a few solutions I see, none of which I like:

1. use the dictsort or something equal in the templating language: I 
don't like this as it doesn't use the underlying DB for the sorting, 
thus is slow (afaic...).

2. add a function:

     def ordered_links(self):
         return self.link_set.all().order_by("name")

to the link class. I don't like this, because if I add another link type 
( say class VerySpecialLinks ) which has the LinkClass as foreign key, 
this function will not work for this special type of link and I'll have 
to write another function in LinkClass.

3. write a view and fill a dict with the info I want, pass the dict then 
print via the template. This seems ridiculous as would be writing the 
same twice (once in python, once in the templating).


Is there a nice AND simple solution for this?


  - bram

-- 
MTG                   - http://www.mtg.upf.edu/
The Freesound Project - http://freesound.iua.upf.edu
Smartelectronix       - http://www.smartelectronix.com
Musicdsp              - http://www.musicdsp.org
Office Telephone      - +34 935 422 101

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to