Home > Software design >  Distinct not working in DRF sqlite database
Distinct not working in DRF sqlite database

Time:02-19

Distinct not working.I am using sqlite in backend

class getPatient(generics.ListAPIView):

    def list(self, request):
        queryset = Patient.objects.all().distinct()
        serializer = PatientSerializer(queryset, many=True)
        return Response({'patient': serializer.data})

I tried :

queryset = Patient.objects.distinct("name").all()

queryset = Patient.objects.values('name').distinct()

queryset = Patient.objects.all().distinct()

Nothing worked

CodePudding user response:

First you have to order objects by the field if you want to distinct. I have included you the documentation and example.

By default, a QuerySet will not eliminate duplicate rows. In practice, this is rarely a problem, because simple queries such as Blog.objects.all() don’t introduce the possibility of duplicate result rows.

Documentation Screen Shot

  • Related