问题描述
当我发出PATCH请求时,我试图部分更新我的ProfileSerializer。但是,由于默认情况下,我无法使其成为默认值,因此Serializer不允许进行部分更改。我正在使用Django Rest Framwork UpdateModelMixin处理我的补丁请求。在我的情况下,我该在哪里设置.owl-dt-container-buttons {
display: flex;
flex-direction: row-reverse;
}
?
查看:
partial=True
序列化器:
class ProfileViewPartialUpdate(GenericAPIView,UpdateModelMixin):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
lookup_field = 'token'
lookup_url_kwarg = 'pk'
def patch(self,request,*args,**kwargs):
return self.partial_update(request,**kwargs)
解决方法
查看:
from rest_framework import generics
class ProfileViewPartialUpdate(generics.RetrieveUpdateAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
lookup_field = 'pk'
序列化器:
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('token','bio','name','email','sport','location','image')
,
在我的一个项目中查看此代码段代码。您可以进行相应的修改。将此添加到ProfileViewPartialUpdate类而不是补丁。
def partial_update(self,request,slug):
user = self.request.user
get_blog = Blog.objects.get(slug=slug)
instance = self.get_object()
serializer = BlogSerializer(instance,data=request.data)
if serializer.is_valid():
serializer.save()
return Response(data=serializer.data,status=HTTP_201_CREATED)
return Response(data="wrong parameters",status=HTTP_400_BAD_REQUEST)
希望这会有所帮助