我正在尝试使用 Django-Rest-Framework (DRF) 创建一个 api 来对用户进行 CRUD。我设法创建并读取了一个用户,但由于某种原因更新不起作用。似乎它正在尝试创建用户而不是更新它,因为它以“用户名已存在”错误消息进行响应。当我尝试传递尚未在数据库中的电子邮件时,它只会创建一个新用户。有谁知道为什么会这样?
这是我的用户序列化器:
class UserSerializer(FriendlyErrorMessagesMixin, serializers.ModelSerializer):
password = serializers.CharField(
max_length=128,
min_length=8,
write_only=True
)
class Meta:
model = User
fields = ('username',
'email',
'password',
'name',
'house_number',
'address_line_one',
'address_line_two',
'address_line_three',
'city',
'postcode',
'phone_number')
read_only_fields = ('date_created', 'date_modified', 'username')
def update(self, instance, validated_data):
password = validated_data.pop('password', None)
for (key, value) in validated_data.items():
setattr(instance, key, value)
if password is not None:
instance.set_password(password)
instance.save()
return instance
这是我的观点:
class UserRetrieveUpdateAPIView(RetrieveUpdateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = UserSerializer
def retrieve(self, request, *args, **kwargs):
serializer = self.serializer_class(request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)