Can`t update data in data base via patch method in django
I have a model of items, and i need to write CRUD operations with data. Post and get works, but patch - no, and i can`t understand why serializers.py
class CreateItemSerializer(serializers.ModelSerializer):
photo = serializers.ImageField(max_length=None, allow_empty_file=True, allow_null=True)
class Meta:
model = CreateItem
fields = '__all__'
def create(self, validated_data):
items = CreateItem.object.create_item(
name = validated_data.get('name'),
description = validated_data.get('description'),
type_item = validated_data.get('type_item'),
photo=validated_data.get('photo')
)
return items
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.description = validated_data.get('description', instance.description)
instance.type_item = validated_data.get('type_item', instance.type_item)
instance.photo = validated_data.get('photo', instance.photo)
instance.save()
return instance
views.py
class CreateItemView(APIView):
serializer_class = CreateItemSerializer
def post(self, request):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(_('item created successfully'), status=status.HTTP_201_CREATED)
def get(self, request, pk, format=None):
item = [item.name for item in CreateItem.object.all()]
description = [item.description for item in CreateItem.object.all()]
type_item = [item.type_item for item in CreateItem.object.all()]
return Response({'name':item[pk], 'description':description[pk], 'type_item':type_item[pk]}, status=status.HTTP_200_OK)
def patch(self, request, pk):
serializer = CreateItemSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(_("item updated successfully"), status=status.HTTP_200_OK)
when i call patch method, "all works" but data doesn`t change
The logic of the save method differ depending on the parameter you delivered when the DRF Serializer object was created.
In the case of the patch method, it is the process of modifying an existing instance.
That's why when you create a Serializer in the patch method, you need to forward not only the data sent by the client, but also the instances that you want to change.
However, when creating a serializer object in your patch method, only the value delivered by the client is passed as an argument(request.data).
class CreateItemView(APIView):
serializer_class = CreateItemSerializer
def patch(self, request, pk):
serializer = CreateItemSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(_("item updated successfully"), status=status.HTTP_200_OK)
And the update method of Serializer is also not called because it does not call the save method of Serializer.
from django.shortcuts import get_object_or_404
class CreateItemView(APIView):
serializer_class = CreateItemSerializer
def patch(self, request, pk):
instance = get_object_or_404(CreateItem, id=pk)
serializer = CreateItemSerializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(_("item updated successfully"), status=status.HTTP_200_OK)
And I think you are trying to proceed with a partial update as you have used the patch method.
If Serializer wants a partial update, you can deliver the partial parameter to True.
from django.shortcuts import get_object_or_404
class CreateItemView(APIView):
serializer_class = CreateItemSerializer
def patch(self, request, pk):
instance = get_object_or_404(CreateItem, id=pk)
serializer = CreateItemSerializer(instance, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(_("item updated successfully"), status=status.HTTP_200_OK)