To start off, I have two models:

class Category(models.Model):
    name = models.CharField(maxlength=50)
    visible = models.BooleanField(default=True)

class Forum(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField(maxlength=50)
    visible = models.BooleanField(default=True)

I want to display a list of the forums, grouped by categories (see
below). If a category is marked invisible, I don't want to show any
forums, even if they are marked visible. If there are no forums in a
category, I still want to display the category title.

<h2>category</h2>
<p>forum name</p>
<p>forum name</p>

<h2>category</h2>
<p>no forums in this category</p>

I have my view and template setup like this:

def index(request):
    categories = Category.objects.filter(visible=True)

{% for category in categories %}
    <h2>{{ category.name }}</h2>
    {% if category.forum_set.count %}
        {% for forum in category.forum_set.all %}
            <p>{{ forum.name }}</p>
        {% endfor %}
    {% else %}
        <p>no forums in this category</p>
    {% endif %}
{% endfor %}

This does hide invisible categories, but the problem is that it shows
all forums, regardless of whether they are marked visible.

Another solution I came up with is to do this in the view:

def index(request):
    forums = Forum.objects.filter(visible=True, category__visible=True)

Using this method, I can just loop through the forums dict and use an
{% ifchanged %} to display the category names. The problem with this is
that I can't display the category name if there are no forums in it.

Is it possible to do what I want? I'm sure theres a way (maybe using
the extra() method), I just can't figure it out.I hope I made my
question clear enough.


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