Home ManytoMany field django template when models are in different apps
Reply: 2

ManytoMany field django template when models are in different apps

sgauri
1#
sgauri Published in 2018-02-11 18:46:48Z

My question is similar to this. I have 2 models linked with m2m field and I want to render the field in template. How can I do this when my 2 models are in different apps:

apps/qapp/models

class Area(models.Model):
    name = models.CharField(max_length=100, primary_key=True)
    def __unicode__(self):
        return self.name

apps/worksheets/models

class Place(models.Model):
    id = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=100, primary_key=True)
    area = models.ManyToManyField('qapp.Area',related_name='area')
Vitor Freitas
2#
Vitor Freitas Reply to 2018-02-11 20:36:05Z

As schwobaseggl mentioned, that fact your models are in different apps doesn't make difference. Rendering fields connected via ManyToMany relationship should be pretty much the same.

If I understood correctly, the problem was that you wasn't using a related_name, and the code snippet you shared wasn't really your actual code.

But just for the sake of completeness, in case someone find this question in the future:

The use of related_name is not mandatory. If the ManyToMany relationship were defined as:

class Place(models.Model):
    # ...
    area = models.ManyToManyField('qapp.Area')

In a Area instance, you would be able to navigate like this:

area = Area.objects.get(pk=1)
places = area.place_set.all()

Because Django automatically adds a reverse relationship %(model_name)_set. If you set a related_name, it will override the default name.

Finally, to enhance the readability of the code, the relationship with the Area model would be better that way:

class Place(models.Model):
    id = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=100, primary_key=True)
    areas = models.ManyToManyField('qapp.Area', related_name='places')

Using the plural form for the ManyToMany relationship. After all, it's a collection of Area and Place on both sides, so areas and places.


Now, to render a ManyToMany field in the template you could do it like this (considering your view returned an Area instance named area:

<h1>Currect area: {{ area.name }}</h1>
<h2>Places:</h2>
<ul>
  {% for place in area.places.all %}
    <p>{{ place.name }}</p>
  {% endfor %}
</ul>
sgauri
3#
sgauri Reply to 2018-02-13 06:53:13Z

There are 2 ways we can show m2m fields data in template.

  1. If you want to show all the places related to an area; see @Vitor's answer.

  2. If you want to show all the areas related to a single place; see below -

queryset for the places place1 = Place.objects.filter(id=1)

Now in the template:

{% for place2 in place1 %}

    {% for area1 in place2.area.all %}

        <p>{{area1.name}}</p>

    {% endfor %}

{% endfor %}

I have deliberately taken variables like above so that someone new can understand where to put which variable.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306689 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO