Django Rest - Error when creating a custom respons

2019-06-19 10:38发布

问题:

I want to implement an email verification and the way I have got it working seems "hacky" but it seems to work good. I've created a custom response in the return portion of the function, but I keep getting the following error:

The response content must be rendered before it can be iterated over.

The process is the standard someone registers and when I go to save the User model I have a send_mail() function that sends out the email with a verification key. The user clicks the link and it passes the key like this:

/api/account/verify/849c40665175e56709855cc7aec2b16c05a4d977b3b083790334c6bc01f6e522

In the view you can see where I pull the key out and process everything. I'm guessing the error is because I'm using Response() in get_queryset(), but I'm not sure. If not, how do I create a custom response message?

Versions

Python==2.7.10
Django==1.11.4
djangorestframework==3.6.3

Model

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), null=False, unique=True)
    first_name = models.TextField(_('first name'), null=False)
    last_name = models.TextField(_('last name'), null=False)
    is_active = models.BooleanField(_('active'), default=False)
    is_admin = models.BooleanField(_('admin'), default=False)
    created_on = models.DateTimeField(_('create on'), auto_now_add=True)
    updated_on = models.DateTimeField(_('updated on'), auto_now=True)
    is_staff = models.BooleanField(_('staff'), default=False)
    activation_key = models.CharField(_('email validation key'), default='', max_length=256)

View

class ActivateViewSet(generics.ListAPIView):

    queryset = User.objects
    serializer_class = ActivationSerializer

    def get_queryset(self):
        activation_key = self.kwargs['activation_key']
        if User.objects.all().filter(activation_key=activation_key).exists():
            User.objects.all().filter(activation_key=activation_key).update(is_active=True)
            return Response({'message': 'key accepted'}, status=status.HTTP_204_NO_CONTENT)
        raise NotFound('activation key not found')

Serializer

class ActivationSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email',)

Traceback

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view 58. return view_func(*args, **kwargs)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/django/views/generic/base.py" in view 68. return self.dispatch(request, *args, **kwargs)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 489. response = self.handle_exception(exc)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception 449. self.raise_uncaught_exception(exc)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 486. response = handler(request, *args, **kwargs)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/generics.py" in get 201. return self.list(request, *args, **kwargs)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/mixins.py" in list 48. return Response(serializer.data)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/serializers.py" in data 739. ret = super(ListSerializer, self).data

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/serializers.py" in data 263. self._data = self.to_representation(self.instance)

File "/Users/*/.virtualenvs/video_env/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation 657. self.child.to_representation(item) for item in iterable

File "/Users/*y/.virtualenvs/video_env/lib/python2.7/site-packages/django/template/response.py" in iter 121. 'The response content must be rendered before it can be iterated over.'

Exception Type: ContentNotRenderedError at /api/account/activate/849c40665175e56709855cc7aec2b16c05a4d977b3b083790334c6bc01f6e522/ Exception Value: The response content must be rendered before it can be iterated over.

回答1:

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)