I have a model (Restrictie) in which those who voted (participanti) and those who voted (votant) are registered. If I forloop in the templates, I can see who voted for whom, but one line is displayed for each record. How can I make a forloop or query in which participants are displayed only once and next to all the voters for example:
name participanti - (name votant 1, name votant 2, ....)
Below my code
models.py
class Participanti(models.Model):
campanievot = models.ForeignKey(Campanie, on_delete=models.CASCADE)
nume_participant = models.CharField(max_length=200, verbose_name='nume')
dep_participant = models.CharField(max_length=200, verbose_name='departament')
votes = models.IntegerField(default=0)
def __str__(self):
return self.nume_participant
class Meta:
verbose_name = 'Participanti'
verbose_name_plural = 'Participanti'
class Restrictie(models.Model):
participanti = models.ForeignKey(Participanti,on_delete=models.CASCADE, verbose_name='Votat')
votant = models.ForeignKey(UserVot,on_delete=models.CASCADE)
def __str__(self):
return str(self.participanti)
class Meta:
verbose_name = 'Voturi efectuate'
verbose_name_plural = 'Voturi efectuate'
views.py
def rezultate(request):
cineavotat = Restrictie.objects.all()
.......
I tried this but it's show all entries like : name 1 = voters 1, name 1 = voters 2 ...
{% for p in cineavotat %}
{{ p.participanti }} - {{ p.votant}}
{% endfor %}
Please help me with a solution. Thank you!