python - Django rest framework: override create() in ModelSerializer passing an extra parameter -
i looking way ovverride default .create()
method of modelserializer
serializer in django rest framework dealing parameter.
in original django model have overridden default.save()
method managing extra
param. .save()
can called in way: .save(extra = 'foo')
.
i have create modelserializer
mapping on original django model:
from originalmodels.models import originalmodel rest_framework import serializers class originalmodelserializer(serializers.modelserializer): # model fields class meta: model = originalmodel
but in way can't pass extra
param model .save()
method.
how can override .create()
method of originalmodelserializer
class take (eventually) extra
param account?
hmm. might not perfect answer given don't know how want pass "extra" in (ie. field in form normally, etc)
what you'd want represent foo field on serializer. present in validated_data
in create
, can make create
following
def create(self, validated_data): obj = originalmodel.objects.create(**validated_data) obj.save(foo=validated_data['foo']) return obj
you'd want @ default implementation of create of other things though (like remove many-to-many relationships, etc.).
Comments
Post a Comment