[Django]-How to use custom serializers fields in my HyeprlinkedModelSerializer

4👍

Just make a copy of serializer_field_mapping from the base class of your serializer and update it with new “model field – serializer” field pair. For example if you use ModelSerializer subclass then:

from rest_framework import serializers
class MySerializer(serializers.ModelSerializer):
    serializer_field_mapping = (
        serializers.ModelSerializer.serializer_field_mapping.copy()
    )
    serializer_field_mapping[models.BinaryField] = MyBinaryField

0👍

One way to do this,

class MySerializer(serializers.Serializer):
    logo = MyBinaryField()

However, I suppose you are asking about overriding model serializer field then you could use the following,

class AccountSerializer(serializers.ModelSerializer):
    logo = MyBinaryField(read_only=True)

    class Meta:
        model = Account

Reference from Docs

Leave a comment