关于python:Django Rest – 创建自定义响应消息时出错

Django Rest - Error when creating a custom response message

我想实现一个电子邮件验证,我的工作方式似乎"黑客",但它似乎工作得很好。我在函数的返回部分创建了一个自定义响应,但我一直收到以下错误:

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

这个过程是有人注册的标准过程,当我保存用户模型时,我有一个send_mail()函数,它用一个验证密钥发送电子邮件。用户单击链接并按如下方式传递键:

1
/api/account/verify/849c40665175e56709855cc7aec2b16c05a4d977b3b083790334c6bc01f6e522

在这个视图中,你可以看到我从哪里拔出钥匙,然后处理所有的事情。我猜错误是因为我在get_queryset()中使用了Response(),但我不确定。如果没有,如何创建自定义响应消息?

版本

1
2
3
Python==2.7.10
Django==1.11.4
djangorestframework==3.6.3

模型

1
2
3
4
5
6
7
8
9
10
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)

视图

1
2
3
4
5
6
7
8
9
10
11
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')

序列化程序

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

回溯

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.


get_queryset期望返回类型是queryset而不是Response对象。您需要将代码更改为

1
2
3
4
5
6
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

现在,由于您希望发送自定义响应,因此无法在get_queryset中执行。必须重写序列化程序的创建方法。下面是一个如何覆盖这个的一般示例。

1
2
3
4
5
6
7
8
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)