我正在通過結合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
如何正確刪除帳戶?
我需要你的幫助。
你能張貼滿回溯? – neverwalkaloner
@neverwalkaloner我補充道。 – xKxAxKx