[英]How can I delete a user account in Django Rest Framework?
I am creating an application by combining Django Rest Framework and Angular. 我正在通过结合Django Rest Framework和Angular创建一个应用程序。
Please let us know about the implementation of user deletion, because there are places that will not work. 请告知我们有关用户删除的实现,因为有些地方无法使用。
I described in Djnago's views.py about the deletion view as follows. 我在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
email is stored in self.request.user
. 电子邮件存储在
self.request.user
。
In addition, I write in serializer.py as follows. 另外,我在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)
In this state, when sending a method of Angular to DELETE to the URL(/api/user/delete/) to which AuthInfoDeleteView is linked, the following error occurred. 在这种状态下,将Angular的DELETE方法发送到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
How can I properly delete accounts correctly? 如何正确正确删除帐户?
I need your help. 我需要你的帮助。
Try editing your view like this, 尝试像这样编辑视图,
try:
instance = self.queryset.get(email=self.request.user.email)
return instance
except Account.DoesNotExist:
raise Http404
Why not disabling the account instead of deleting it? 为什么不禁用帐户而不是删除帐户?
That was my approach. 那是我的方法。 In my view class (which I use for the extended UserProfile and inherits from RetrieveUpdateDestroyAPIView) I overwrite the destroy method like this:
在我的视图类(用于扩展的UserProfile并从RetrieveUpdateDestroyAPIView继承的视图类)中,我覆盖了destroy方法,如下所示:
def destroy(self, request, pk=None, **kwargs):
request.user.is_active = False
request.user.save()
return Response(status=204)
I use Token authentication (using rest_auth) and after disabling the account, tokens are no longer valid (the response when trying to use them is "User inactive or deleted". 我使用令牌身份验证(使用rest_auth),并且在禁用帐户后,令牌不再有效(尝试使用令牌时,响应为“用户未激活或已删除”)。
I think its best practice to disable accounts instead of deleting them. 我认为最好的做法是禁用帐户而不是删除帐户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.