AAA
AAA

Reputation: 2032

Displaying subsets of objects for Many to Many in Django Template

This is what part of my models look like:

class Character(models.Model):
    name = models.CharFied()

class Issue(models.Model):
    character = models.ManyToManyField(Character, related_name="appearances", blank=True, null=True)

What I'm trying to do is, when you go to a character's page, it will show all the Issues that Character is in.

This is how my view is setup:

def character(request, character_slug):
    character = get_object_or_404(Character, slug=character_slug)            
    return render_to_response('comics2/character_detail.html', {'character': character}, context_instance=RequestContext(request)

In the template:

{{ character.name }}

works

I tried this:

class CharactersView(ListView):
    context_object_name = "character_list"
    template_name = "comics2/character_detail.html"    
    def get_queryset(self):
        character = get_object_or_404(Character, slug=self.kwargs['character_slug'])
        return Issue.objects.filter(character=character)

But it doesn't work..

But how would I go about in the template displaying all the issues that character has appeared in? For example:

Character Name
Issue #1
Issue #2,
etc.

I feel this is something very simple, but I'm not getting it. I've scoured the Internet for answers, but can't seem to find things that work. So, let me know what's going and I prefer baby talk and possibly the actual code...thanks!

Upvotes: 0

Views: 1495

Answers (1)

dting
dting

Reputation: 39287

You are looking for following a relationship backwards

{% for issue in character.appearances.all %}
    {{ issue }}
{% endfor %}

since you set a related name, otherwise, character.issue_set.all is the default. You can also add the issues to the context by getting the related set in the view.

full example:

models.py

class Character(models.Model):
    name = models.CharField()
    slug = models.SlugField()

    def __unicode__(self):
        return "%s" % self.name

class Issue(models.Model):
    title = models.CharField()
    volume = models.IntegerField()
    character = models.ManyToManyField(Character, related_name="appearances", blank=True, null=True)

    def get_absolute_url(self):
        ...

views.py

def character(request, character_slug):
    character = get_object_or_404(Character, slug=character_slug)
    issues = character.appearances.all()

    context = {'character': character,
               'issues':issues,}                           

    return render_to_response('comics2/character_detail.html', 
        context_instance=RequestContext(request)

template.html

<h1>Appearances of {{ character }}<h1>
<table>
    <thead>
        <tr>
            <th>Title</th>
            <th>Volume</th>
        </tr>
    </thead>
    <tbody>
        {% for issue in issues %}
        <tr>
            <td><a href="{{ issue.get_absolute_url }}>{{ issue.title }}</a></td>
            <td>{{ issue.volume }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

Upvotes: 3

Related Questions