I’ll use a slightly simplified version of the example from Djano’s docs.
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class MembershipInfo(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
invite_reason = models.CharField(max_length=64)
So what I want to do is iterate through say, Group and Person in the template and get some of the info from the extra fields like so:
{% for group in group_list %}
{% for person in group.person.all %}
{{ person.membership_info.invite_reason }}
{% endfor %}
{% endfor %}
But this doesn’t seem to work for me. I suppose I just can’t find out the right way to access it.
You can try the following:
It is not very efficient, but it’s the only option I can think of that uses solely templates.
Edit. Another option is to select all MembershipInfo objects from the database, sort them by person and group and pass them to your template. That would need only one database query.