B
B
Bjornie2017-08-26 13:35:34
Django
Bjornie, 2017-08-26 13:35:34

How to combine 3 identical (almost) cycles in a template and display a common ordinal number?

To make it clearer, I will make an example of a model and my template below.

Codes
template.html:
{% for match in matches %} 
  <div class="match-col">
    <div class="match-col__names">
      <table class="match-table">
        {% for para in match.matchparainstance_set.all %}
          <tr class="match-table__tr">
            <td class="order">ПОРЯДКОВЫЙ ID</td>
            <td class="name">{{ para.para.person }}</td>
          </tr>
        {% endfor %}
        {% for pag in match.matchpaginstance_set.all %}
          <tr class="match-table__tr">
            <td class="order">ПОРЯДКОВЫЙ ID}</td>
            <td class="name">{{ pag.pag.person }}</td>
          </tr>
        {% endfor %}
        {% for pass in match.team.passenger.all %}
        <tr class="match-table__tr">
          <td class="order">ПОРЯДКОВЫЙ ID</td>
          <td class="name">{{ pass.person }}</td>
        </tr>
        {% endfor %}
      </table>
    </div>
  </div>
{% endfor %}

models.py
class Match(models.Model):
    date = models.DateField()
    match_name = models.CharField(max_length=255)
    team = models.ForeignKey('Team', blank=True, null=True)

class MatchPARAInstance(models.Model):
    match = models.ForeignKey('Match')
    para = models.ForeignKey('PARA')

class MatchPAGInstance(models.Model):
    match = models.ForeignKey('Match')
    pag = models.ForeignKey('PAG')

class Team(models.Model):
    team_name = models.CharField(max_length=255)
    passenger = models.ManyToManyField(PARA)

In the admin panel, I add MatchPAGInstance and MatchPARAInstance to the Match model through inlines. To access the PARA\PAG Instances in the template, I loop through _set.all .
My question is how to combine this data to get a common serial number for all objects at once (the length of all objects, not the current cycle), and not for each cycle it started from the beginning. For example, now it is displayed like this: PARA: 1,2,3; PAG: 1.2; Team: 1,2,3,4,5 because the list is ultimately the same (like the template), although the data type is different.
Models with the Instance prefix have FKs for models that have different field sets, so they are different. Team, in turn, consists of objects of the PARA model.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-08-26
@deliro

zip + forloop.counter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question