[Fixed]-What is the best way to manage custom serializer in DRF

1👍

Override the create() function for serializers.Serializer. And to manage updates, override the update() function.

class CustomSerializer(serializers.Serializer):
    a_model_serializer = AModelSerializer()
    b_model_serializer = serializers.ListField(child=CModelSerializer())
    another_field = serializers.CharField()

    def create(self, validated_data):
       print(validated_data)
       # Do your custom create

    def update(self, instance, validated_data):
       print(validated_data)
       # Do your custom update

http://www.django-rest-framework.org/api-guide/serializers/#saving-instances

👤zubhav

Leave a comment