[Answered ]-Django rest framework add field in serializer for using with post method only

2👍

Whoever wrote the code probably didn’t need browsable api (normal requests will work fine).

In order to fix the api change to_native to this:

def to_native(self, obj):
    """
    Serialize objects -> primitives.
    """
    ret = self._dict_class()
    ret.fields = self._dict_class()

    for field_name, field in self.fields.items():

        if field.read_only and obj is None:
            continue

        field.initialize(parent=self, field_name=field_name)
        key = self.get_field_key(field_name)
        value = field.field_to_native(obj, field_name)

        method = getattr(self, 'transform_%s' % field_name, None)
        if callable(method):
            value = method(obj, value)

        if field_name not in self.opts.postonly_fields:
            ret[key] = value

        ret.fields[key] = self.augment_field(field, field_name, key, value)
    return ret

Leave a comment