Query Django without repeating results

0

Good afternoon I use Django 2.0 in an application to control attendees to an event, the idea is to generate a report of attendees, using the Assistance model, regardless of having more than one record, and the result of the query thrown me people without repeating them.

models.py

class Persona(models.Model):
    nombres = models.CharField(max_length=50)
    apellidos = models.CharField(max_length=50)
    numeroDocumento = models.BigIntegerField()
    telefono = models.CharField(max_length=15,null=True)
    email = models.EmailField()

class Asistencia(models.Model):
    persona = models.ForeignKey(Persona, on_delete=models.CASCADE)
    hora = models.TimeField(default=time.strftime("%H:%M:%S"),editable=False)
    fecha = models.DateField(default=date.today,editable=False)

views.py

personas = Asistencia.objects.filter(fecha__range=[fi,ff])

I've tried distinct but I do not get the expected result

    
asked by jhon1946 26.05.2018 в 00:07
source

2 answers

0

the distinction must be used with the voucher, for example:

lista_personas = Asistencia.objects.filter(fecha__range=[fi,ff]).values("persona").distinc()

personas = Persona.objects.filter(pk__in=lista_personas)
    
answered by 28.05.2018 / 23:07
source
0

If what you are looking for is to obtain people, you could make the following query:

personas = Persona.objects.filter(asistencia__fecha__range=[fi,ff]).distinct()

This will generate the JOIN with Asistencia and apply the range filter, finally apply the distinct to avoid repeated people.

I hope I have been helpful.

Greetings!

    
answered by 28.05.2018 в 02:24