[Fixed]-Error when creating a custom response message

16👍

get_queryset expects a return type to be a queryset not a Response object. You need to change your code to something like

def get_queryset(self):
        activation_key = self.kwargs['activation_key']
        if User.objects.all().filter(activation_key=activation_key).exists():
            users = User.objects.all().filter(activation_key=activation_key)
                                      .update(is_active=True)
            return user

Now since you want to send a custom response it can’t be done in get_queryset. You have to override your serializer’s create method. Below is a general example for how to override this.

def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if not serializer.is_valid(raise_exception=False):
            return Response({"Fail": "blablal", status=status.HTTP_400_BAD_REQUEST)

        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response({"Success": "msb blablabla"}, status=status.HTTP_201_CREATED, headers=headers)

Leave a comment