On Fri, Sep 12, 2008 at 2:36 AM, Lance F. Squire <[EMAIL PROTECTED]> wrote:
>
> Currently using Django version 0.96.3, on a Fedora 8 system.
>
> I've currently set-up models for 'Manufacturer' and 'System'. System
> has a Foreignkey to Manufacturer.
>
> I'd like to display a list like so:
>
> Manu A
>   System a
>   System b
>   System c
>
> Manu B
>   System a
>   System b
>   System c
>
> Etc...
>
> Only listing Manufacturers that had systems of type X.
>
> I was thinking of using code like this in the View,
>
>  man=Manufacturer.objects.all()
>>>> for m in man:
> ...    sys=System.objects.filter(manufacturer=m.id)
> ...    print m.name
> ...    for s in sys:
> ...       print "  %s" % s.name
> ...
>
> This works from the shell, but I'm unsure how to get the output to the
> template And, I suspect is a terribly inefficient way to do it anyway.

Sounds like you're looking for something like the following:

{% for m in man %}
   {{ m.name }}
   {% for s in m.sys_set.all %}
      {{ s.name }}
   {% endfor %}
{% endfor %}

(plus some formatting bits, but they should be pretty obvious). This
assumes that "man" is in your context, and contains a list of
manufacturers (Manufacturer.objects.all(), for instance). The only
piece of this puzzle that might be a little confusing is
'm.sys_set.all'. m.sys_set is Django's way of saying "get all the sys
objects that are related to the Manufacturer m". More details here:

http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

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

Reply via email to