Django: show a ManyToManyField in a template?

Django

Django Problem Overview


I've got these models in my Django project:

class Area(models.Model):
    name = models.CharField(max_length=100, primary_key=True)
    def __unicode__(self):
        return self.name
class Place(models.Model):
    id = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=100, primary_key=True)
    area = models.ManyToManyField(Area,related_name='area')

How can I show the Place's area name(s) in my template? Currently I have:

{% for place in places %}
    Name: {{ place.name }}, Area: {{ place.area}}
{% endfor %}

which gives:

Area: <django.db.models.fields.related.ManyRelatedManager object at 0x10435a3d0>

And {{ place.area}} is just blank. Can anyone help?

Django Solutions


Solution 1 - Django

Use place.area.all in the template
http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

{% for place in places %}
    Name: {{ place.name }}<br/>
    Area: <br/>{% for area in place.area.all %}{{ area }}<br/>{% endfor %}
{% endfor %}

Solution 2 - Django

You can use the existing join template tag.

https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#join

Here's the code

{% for place in places %}
    Name: {{ place.name }}, Area: {{ place.area.all|join:", " }}
{% endfor %}

Solution 3 - Django

What does your view code look like?
Here's one way you can return the related models:

from myapp.models import Area, Place

def detail(request, place_id):
    place = Place.objects.get(pk=place_id)
    areas = place.area.all()

    return render_to_response('detail.html', {
        "place": place,
        "areas": areas,
    })

This example is just for illustration; you'd want to include error-handling code.
Your template might look something like this:

<h3>{{ place }}</h3>

{% if areas %}
  <ul>
  {% for area in areas %}
    <li>{{ area.name }}</li>
  {% endfor %}
  </ul>
{% endif %}

Solution 4 - Django

For show only ManyToMany field:

{% for place in places.area.all %}
    {{ place.name }}
{% endfor %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAP257View Question on Stackoverflow
Solution 1 - DjangocrodjerView Answer on Stackoverflow
Solution 2 - DjangoJohn Paulo RodriguezView Answer on Stackoverflow
Solution 3 - Djangomechanical_meatView Answer on Stackoverflow
Solution 4 - DjangonamjooView Answer on Stackoverflow