SHARE
TWEET

Untitled

a guest Jun 17th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
  2.     response = self.handle_exception(exc)
  3.   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
  4.     self.raise_uncaught_exception(exc)
  5.   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
  6.     response = handler(request, *args, **kwargs)
  7.   File "./api/views.py", line 76, in patch
  8.     return super().update(new_password, kwargs['pk'])
  9.   File "/usr/local/lib/python3.6/site-packages/rest_framework/mixins.py", line 68, in update
  10.     serializer = self.get_serializer(instance, data=request.data, partial=partial)
  11. AttributeError: 'str' object has no attribute 'data'
  12.      
  13. class ChangePassView(generics.RetrieveUpdateAPIView):
  14.  
  15.     queryset = User.objects.filter.all()
  16.     serializer_class = ChangePassSerializer
  17.  
  18.     def patch(self, request, *args, **kwargs):
  19.  
  20.         old_password = request.POST["old_password"]
  21.         new_password = request.POST["new_password"]
  22.         new_password_confirm = request.POST["new_password_confirm"]
  23.  
  24.         if new_password != (new_password_confirm):
  25.              return JsonResponse({'message': '新しいパスワードが異なります'}, status=403)
  26.  
  27.         user = User.objects.get(id=kwargs['pk'])
  28.  
  29.         if not user.check_password(old_password):
  30.              return JsonResponse({'message': '現在のパスワードが異なります'}, status=403)
  31.  
  32.         return super().update(new_password, kwargs['pk'])
  33.      
  34. class UpdateModelMixin(object):
  35.     """
  36.     Update a model instance.
  37.     """
  38.     def update(self, request, *args, **kwargs):
  39.         partial = kwargs.pop('partial', False)
  40.         instance = self.get_object()
  41.         serializer = self.get_serializer(instance, data=request.data, partial=partial)
  42.         serializer.is_valid(raise_exception=True)
  43.         self.perform_update(serializer)
  44.  
  45.         if getattr(instance, '_prefetched_objects_cache', None):
  46.             # If 'prefetch_related' has been applied to a queryset, we need to
  47.             # forcibly invalidate the prefetch cache on the instance.
  48.             instance._prefetched_objects_cache = {}
  49.  
  50.         return Response(serializer.data)
  51.  
  52.     def perform_update(self, serializer):
  53.         serializer.save()
  54.  
  55.     def partial_update(self, request, *args, **kwargs):
  56.         kwargs['partial'] = True
  57.         return self.update(request, *args, **kwargs)
  58.      
  59. class User(AbstractBaseUser, PermissionsMixin):
  60.     user_Id = models.CharField(_('user_Id'), max_length=7, unique=True)
  61.     password = models.CharField(_('password'), max_length=255)
  62.      
  63. class ChangePassSerializer(serializers.ModelSerializer):
  64.  
  65.     old_password = serializers.CharField(max_length=255)
  66.     new_password = serializers.CharField(max_length=255)
  67.     new_password_confirm = serializers.CharField(max_length=255)
  68.  
  69.     class Meta:
  70.         model = User
  71.         fields = ('password',)
  72.  
  73.     def update(self, user, validated_data):
  74.         user.set_password(validated_data['password'])
  75.         user.save()
  76.         return user
  77.      
  78. urlpatterns = [
  79.   app_name = 'api'
  80.   path('password_change/<int:pk>/', ChangePassView.as_view(),
  81.         name='password_change'),
  82. ]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top