简体   繁体   English

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

[英]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.

相关问题 如何使用Django Rest Framework向用户添加汽车 - How can I add a car to a user with Django Rest Framework 如何使用每个帐户(而非用户)具有API密钥的Django Rest框架? - How do I use an Django Rest Framework with API key per account (not user)? 在 django 的 rest 中删除用户帐户 - User account delete in django's rest 验证用户帐户是否在 Django Rest Framework 中处于活动状态 - Validate if user account is active in Django Rest Framework 在Django 1.8.2中,如何为用户帐户分配“任务”模型并让用户删除自己的任务? - In Django 1.8.2, how can I assign a “Task” model to a user account and let the user delete their own tasks? 如何使用Django REST框架删除具有特定电子邮件的用户 - How to delete a User with particular email using Django REST framework django-rest-auth:什么是电子邮件验证,如何让用户正确激活帐户? - django-rest-auth: What is the email verification for and how can I let the user activate the account correctly? 如何使用 django rest 框架为不同的用户类型创建自定义用户模型 - How can I create custom user model for different user types using django rest framework 使用django rest框架时,如何强制将用户ID作为值? - How can I force the user id as value when working with django rest framework? 如何允许用户仅在django rest框架中修改其数据? - How I can allow a user to modify only their data in django rest framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM