2017-06-24 60 views
0

我正在通過結合Django Rest Framework和Angular創建一個應用程序。
請讓我們知道用戶刪除的實現,因爲有些地方不起作用。

我在Djnago的views.py中描述瞭如下的刪除視圖。如何刪除Django Rest Framework中的用戶帳戶?

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) 

在該狀態下,在發送的角的方法刪除與URL(/ API /用戶/刪除/),其AuthInfoDeleteView被鏈接時,出現下面的錯誤。

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 

如何正確刪除帳戶?
我需要你的幫助。

+1

你能張貼滿回溯? – neverwalkaloner

+0

@neverwalkaloner我補充道。 – xKxAxKx

回答

1

嘗試編輯您的看法是這樣,

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

爲什麼不禁用帳戶,而不是將其刪除?

這是我的做法。在我的視圖類(我使用用於擴展用戶配置和從RetrieveUpdateDestroyAPIView繼承)我覆蓋破壞方法是這樣的:

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

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

    return Response(status=204) 

我使用令牌認證(使用rest_auth)和禁用帳戶之後,令牌不再有效(試圖使用它們時的響應是「用戶無效或已刪除」。

我認爲最好的做法,而不是禁用刪除他們的帳戶。