Hello
I have this many-to-many model:
class Registers(models.Model):
class Meta:
verbose_name_plural = 'Registers'
lesson_at = models.DateField(help_text="Data da Aula:", verbose_name="Data da Aula:")
regclasseid = models.ForeignKey(Turmas, on_delete=models.CASCADE, verbose_name="Classe:")
regalunoid = models.ForeignKey(Alunos, on_delete=models.CASCADE, help_text="Aluno:", verbose_name="Aluno:")
status = models.CharField(max_length=3, verbose_name="Status:")
def __int__(self):
return self.Alunos.nome
My view looks like this:
def my_view(request):
items = Turmas.objects.all()
related_records = []
if 'item_id' in request.GET:
item_id = request.GET['item_id']
related_records = Registers.objects.filter(regclasseid=item_id)
return render(request, 'schooladmin/registers_report.html', {'items': items, 'related_records': related_records})
And the template is renderer like this:
Here I can see the names of the users are duplicated, which is fine but I just want to show one record, a distinct record.
If I change the view to get only the .disitinct() .values() I can only see the id of the fields:
def my_view(request):
items = Turmas.objects.all()
related_records = []
students = []
if 'item_id' in request.GET:
item_id = request.GET['item_id']
related_records = Registers.objects.filter(regclasseid=item_id)**.values('regalunoid').distinct()**
return render(request, 'schooladmin/registers_report.html', {'items': items, 'related_records': related_records})
Like this:
Why is this happening and how can I make the names to show instead of the id’s?
Thanks in advance,