[EMAIL PROTECTED] wrote:
> Hi,
> 
> Using the models below, I want a single page to show:
> 
> bookcategory1
>       book1
>       book2
> bookcategory2
>       book1
>       book2
>       ...
> bookcategory3
>       ...
> ...
>       ...
> 
> ##################################
> 
> # Models
> 
> class BookCategory(models.Model):
>       ...
> 
> class Book(models.Model):
>       ...
>       category = models.ForeignKey(BookCategory)
>       ...
> 
> ##################################
> 
> What would be the preferred way to do this?
> Can it be done without templatetags? How?
> 
> If the solution must rely on templatetags, can you give me a short
> example of how this templatetag might look?
> 
> Thanks very much!
> 
> Best regards,
> Cello

If you pass the complete list of BookCategories to your template as 
"categories":

<dl>
{% for category in categories %}
   <dt>{{ category }}</dt>
   {% for book in category.book_set.all %}
   <dd>{{ book }}</dd>
   {% endfor %}
{% endfor %}
</dl>

The .all may or may not be necessary - I don't have any code at hand to 
test this :)

If you add related_name="books" to the definition of the category field 
in your Books model, you could use {% for book in category.books.all %} 
in the inner loop.

See http://www.djangoproject.com/documentation/db_api/#related-objects

Jonathan.

--~--~---------~--~----~------------~-------~--~----~
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