PUT请求不更新数据-Django

PUT Request not Updating data-Django

我正在使用 Postman 来获取我的请求。我的 GET 和 POST 似乎工作正常。只有当我用 PUT 去更新数据时,我所在的位置 运行 才会打嗝。邮差实际上发送回数据,就好像对象正在更新一样,但是当我通过 GET 检查时,它与以前的数据相同。我尝试将配置单元数据添加到 serializer.save,但它告诉我我添加了太多参数。

models.py

class Inspection(models.Model):
     hive = models.ForeignKey(Hive, on_delete=models.CASCADE)
     user = models.ForeignKey(User,on_delete=models.CASCADE)
     eggs = models.IntegerField()
     larvae = models.IntegerField()
     sealed_brood = models.IntegerField()
     covered_bees = models.IntegerField()
     nectar_honey = models.IntegerField()
     pollen = models.IntegerField()
     pest_spotted = models.CharField(max_length=200)
     pest_action = models.CharField(max_length=200)
     notes_concerns = models.CharField(max_length=300)

serializers.py

class InspectionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Inspection
        fields = ['id', 'eggs', 'larvae', 'sealed_brood', 'covered_bees', 'nectar_honey', 'nectar_honey', 'pollen', 'pest_spotted', 'pest_action', 'notes_concerns','user_id','hive','hive_id']
        depth = 1

    hive_id = serializers.IntegerField(write_only=True)

Views.py

@api_view(['GET', 'POST','PUT'])
@permission_classes([IsAuthenticated])
def inspection_details(request, pk):
    hive = get_object_or_404(Hive, pk=pk)
    inspection = Inspection.objects.filter(hive_id = hive.id, user=request.user)
    if request.method == "GET":
        serializer = InspectionSerializer(inspection, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
        
    elif request.method == 'POST':
        serializer = InspectionSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            serializer.save(user=request.user)
            return Response(serializer.data,status.HTTP_200_OK)
    
    
    elif request.method == 'PUT':
        serializer = InspectionSerializer(hive, data=request.data)
        if serializer.is_valid(raise_exception=True):
            serializer.save(user=request.user)
            return Response(serializer.data, status=status.HTTP_200_OK)

我认为实例变量不正确,应该是inspection而不是hive

@api_view(['GET', 'POST', 'PUT'])
@permission_classes([IsAuthenticated])
def inspection_details(request, pk):
    hive = get_object_or_404(Hive, pk=pk)
    inspection = Inspection.objects.filter(hive_id = hive.id, user=request.user)
    ...
    elif request.method == 'PUT':
        serializer = InspectionSerializer(inspection, data=request.data)   # here
        if serializer.is_valid(raise_exception=True):
            serializer.save(user=request.user)
            return Response(serializer.data, status=status.HTTP_200_OK)

首先不要对 is_valid 使用 if 子句。无论如何它都会引发异常,所以如果不需要的话。

接下来你需要更新你的序列化器而不是保存它。

 elif request.method == 'PUT':
    serializer = InspectionSerializer(hive, data=request.data)
    serializer.is_valid(raise_exception=True)
    serializer.update(object_to_update,serializer.validated_data)
    # if you want to return updated serialized data :
    updated_serializer = InspectionSerializer(object_to_update)
    return Response(updated_serializer.data, status=status.HTTP_200_OK)