So 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. Any help will be greatly appreciated here.
models
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**
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
@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)
CodePudding user response:
I think the instance variable is not correct, it should be inspection
not 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)
Hope it could help.
CodePudding user response:
first of all don't use if clause for is_valid. it raise exception anyway so if is not needed.
next you need to update your serializer not saving it .
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)