[Fixed]-Validation on query_params in Django Rest Framework

6👍

With this function:

  def get_queryset(self):
        queryset = ModelName.objects.all()
        name = self.request.query_params.get('name', None)
        if name:
            queryset = queryset.filter(Q(name__icontains=name) | Q(abbreviation__icontains=name)).all()
        else:
            raise exceptions.ParseError("name not supplied")
        return queryset

You should make sure you are always returning a queryset (or raising an exception if that is how you want to handle it).

👤djq

Leave a comment