Skip to content
Advertisement

I want my django rest framework serializer to accept input but not add to model

I removed some fields from my model, but I want the serializer to still accept the fields as input. How do I have fields the serializer accepts but doesn’t use?

class EventBaseSerializer(ModelSerializer):
    class Meta:
        model = models.Event
        fields = ("id", "name")

        #unused_fields = ("last_name")

Advertisement

Answer

From http://www.django-rest-framework.org/api-guide/serializers/

You can add extra fields to a ModelSerializer or override the default fields by declaring fields on the class, just as you would for a Serializer class.

class AccountSerializer(serializers.ModelSerializer):
    url = serializers.CharField(source='get_absolute_url', read_only=True)
    groups = serializers.PrimaryKeyRelatedField(many=True)`

    class Meta:
        model = Account
Advertisement