[Answered ]-Using UpdateModelMixin update() to update record

1👍

You just have to create a put() function inside your view, while inheriting GenericAPIView and UpdateModelMixin

class MyView(GenericAPIView, UpdateModelMixin):

    serializer_class = MySerializer
    queryset = MyModel.objects.all()
    lookup_field = 'id'

    def put(self, request, *args, **kwargs):

        return self.update(request, *args, **kwargs)

You should create your serializer to contain only the fields you want to update

serializers.py:

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = ["field_1", "field_2", "field_3"]

Take into consideration that any field that has null=False in the model will be required and not optional

You can dodge this by adding

extra_kwargs = {"field_x": {"required": False}}

in your Meta class

Leave a comment