Answer the question
In order to leave comments, you need to log in
Django Interaction between m2m models. How to properly organize and output in a template?
There are two models.
class Stage(models.Model):
stageNumber = models.IntegerField()
stageName = models.CharField()
beginingDate = models.DateField()
endDate = models.DateField()
sportsmens = models.ManyToManyField(Person )
def __str__(self):
return f'{self.stageName}'
class Person(models.Model):
class Meta:
abstract = True
JobNameFull = models.CharField()
JobNameShort = models.CharField()
sportRank = models.ForeignKey(SportRank,)
personFIO = models.CharField()
def __str__(self):
return f'{self.personFIO}'
class StageList(ListView):
model = Stage
def get_queryset(self):
return Stage.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page_title'] = 'Сведения по этапам'
return context
<table id="example" class="table display table-bordered table-striped">
<thead>
<tr>
<th>Порядковый номер этапа</th>
<th>Наименование этапа</th>
<th>Начало этапа</th>
<th>Окончание этапа</th>
<th>Участвующие спортсмены</th>
</tr>
</thead>
<tbody>
{% for item in object_list %}
<tr>
<td>{{ item.stageNumber }}</td>
<td>{{ item.stageName }}</td>
<td>{{ item.beginingDate }}</td>
<td>{{ item.endDate }}</td>
<td>{{ item.??? }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Answer the question
In order to leave comments, you need to log in
{% for person in item.sportsmen.all %}
{{ person.JobNameFull }}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question