1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] PUT Request not Updating data-Django

Discussão em 'Python' iniciado por Stack, Novembro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Postman to get my request. My GET and POST appear to be working fine. It's only when I go to update the data with PUT that its where I am running into the hiccup. Postman actually sends data back as if the object is being updated, but when I go to check via GET it's the same data as before. I have tried adding the hive data to the serializer.save, but it tells me I'm adding too many parameters.

    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)

    Continue reading...

Compartilhe esta Página