Home > OS >  Django distinct selection
Django distinct selection

Time:10-26

i have this model:

class Person:
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    sexe = models.TextChoices('M', 'F')
    arrival_date = models.DateField(max_length=30)
    reason = models.CharField(max_length=30)

It turns out that the same person can be registered several times (only the arrival date and the reason change). I would like to make a query that lists distinctly persons. For example, if a person is registered many times, he will be selected only once.

How can i do it ? Thanks.

CodePudding user response:

you can get data in this way:

Person.objects.values_list('first_name', 'last_name', 'sexe').distinct()

CodePudding user response:

for mysql

Person.objects.filter(positive = 1).order_by().values('first_name').distinct()

for Postgre:

Person.objects.order_by('first_name').distinct('first_name')

Documentation Here

  • Related