Q:

Render ManytoManyfield in django templates without duplicating the content of the parent Model

I have great difficulty on this one.
I am trying to render my template like this following:
Company name A:
-> postalcode 1
-> postalcode 2
and I am have this result for now:
Company name A:
-> postalcode 1
Company name A:
-> postalcode 2
I know it is from the for loop that I have over cp but I don't how to access to the M2M field to show it like I would like.
(and I have an additional m2m field so it would be another for loop)
Here is my code:
models.py
class Company(models.Model):
    utilisateur = models.ForeignKey(User)
    nom_cpny = models.CharField(max_length=200)
    code_postal_cpny = models.ManyToManyField('Place', blank=True)
    gestion_cpny = models.ManyToManyField('Companytype', blank=True)

    def __unicode__(self):
            return self.nom_cpny

class Place(models.Model):
    postalcode = models.CharField(max_length=200)
    ville = models.CharField(max_length=200)
    region = models.CharField(max_length=200)
    departement = models.CharField(max_length=200)
    longitude = models.DecimalField(max_digits=9, decimal_places=6)
    latitude = models.DecimalField(max_digits=9, decimal_places=6)
    pays = models.CharField(max_length=100)

    def __unicode__(self):
        return unicode(self.postalcode)

class Companytype(models.Model):

    cpny_type = models.CharField(max_length=100, blank=False) 
    employee_base = models.IntegerField(blank=False)

    def __unicode__(self):
        return self.cpny_type

views.py
@login_required
def listing_cpny(request):

    cpny_in = Company.objects.all().filter(utilisateur=request.user)
    utilisateur = request.user    

    args = {}
    args.update(csrf(request))

    args['cpny_in'] = cpny_in
    args['utilisateur'] = utilisateur

    return render_to_response('b_dashboard_annonces.html', args) views.py

b_dashboard_annonces.html
{% for annonce in cpny_in %}
    {% for cp in annonce.code_postal_cpny.all %}
    {% if annonce.utilisateur.id == utilisateur.id %}

    <td>{{annonce.nom_cpny}}</td>
    <td>{{cp }}</td>

    </tr>
    {% endif %}
    {% endfor %}
{% endfor %}

Many thanks for your help!

A:

This should work for you
{% for annonce in cpny_in %}
    <tr>
        <td>{{annonce.nom_cpny}}</td>

        {% for cp in annonce.code_postal_cpny.all %}
            {% if annonce.utilisateur.id == utilisateur.id %}
                <td>{{cp }}</td>
            {% endif %}
        {% endfor %}

    </tr>
{% endfor %}

