为什么PATCH返回以回复我想保存的正确数据但是它不会将这些数据保存在数据库中的任何想法?
from .serializers import ObjectSerializer
@permission_classes([UserPermission])
class ObjectDetail(GenericAPIView):
serializer_class = ObjectSerializer
def patch(self, request, object_id):
try:
object = Object.objects.get(id=object_id)
except Object.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = ObjectSerializer(object, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py:
class ObjectSerializer(serializers.ModelSerializer):
class Meta:
model=Object
fields = '__all__'
models.py:
class Object(models.Model):
field1 = models.CharField(max_length=200)
field2 = models.ForeignKey('auth.User')
field3 = models.FloatField()
field4 = models.FloatField(null=True, blank=True)
def save(self, *args, **kwargs):
if not self.pk:
self.field4 = self.field3
super(Object, self).save(*args, **kwargs)
更新
我找到了我的PATCH方法不起作用的原因。问题在于模型中的def save(self, *args, **kwargs):
函数。当我评论它一切正常。任何想法我该如何解决?