如何在Django Rest Framework中删除用户帐户?

时间:2017-06-24 10:25:19

标签: django django-views django-rest-framework

我正在通过组合Django Rest Framework和Angular创建一个应用程序 请告知我们用户删除的实施情况,因为有些地方无法使用
我在Djnago的views.py中描述了删除视图,如下所示。

class AuthInfoDeleteView(generics.DestroyAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = AccountSerializer
    lookup_field = 'email'
    queryset = Account.objects.all()

    def get_object(self):
        try:
            instance = self.queryset.get(email=self.request.user)
            return instance
        except Account.DoesNotExist:
            raise Http404

电子邮件存储在self.request.user 另外,我在serializer.py中写如下。

from django.contrib.auth import update_session_auth_hash
from rest_framework import serializers

from .models import Account, AccountManager


class AccountSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = Account
        fields = ('id', 'username', 'email', 'profile', 'password', )

    def create(self, validated_data):
        return Account.objects.create_user(request_data=validated_data)

在这种状态下,当将一个Angular方法发送到与AuthInfoDeleteView链接的URL(/ api / user / delete /)时,发生了以下错误。

Internal Server Error: /api/user/delete/
Traceback (most recent call last):
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/rest_framework/generics.py", line 219, in delete
    return self.destroy(request, *args, **kwargs)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/rest_framework/mixins.py", line 93, in destroy
    self.perform_destroy(instance)
  File "/Users/user/anaconda/envs/auth_django_and_angular2/lib/python3.6/site-packages/rest_framework/mixins.py", line 97, in perform_destroy
    instance.delete()
TypeError: 'bool' object is not callable
[24/Jun/2017 14:04:42] "DELETE /api/user/delete/ HTTP/1.1" 500 15690

如何正确删除帐户?
我需要你的帮助。

2 个答案:

答案 0 :(得分:1)

尝试像这样编辑你的视图,

try: 
    instance = self.queryset.get(email=self.request.user.email) 
    return instance 
except Account.DoesNotExist: 
    raise Http404

答案 1 :(得分:0)

为什么不禁用该帐户而不是删除它?

这是我的方法。在我的视图类中(我用于扩展的UserProfile并从RetrieveUpdateDestroyAPIView继承)我覆盖了这样的destroy方法:

def destroy(self, request, pk=None, **kwargs):

    request.user.is_active = False
    request.user.save()

    return Response(status=204)

我使用令牌身份验证(使用rest_auth),并且在禁用帐户后,令牌不再有效(尝试使用它们时的响应是“用户无效或已删除”。

我认为最佳做法是禁用帐户而不是删除帐户。