Open
Description
Django's ModelForm
sets all specified fields to None
when you don't pass in the data. It's possible that this doesn't happen when using a DRF serializer instead (I haven't tried it). Overriding the ModelForm
's __init__
method like this seems to work:
class BaseModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super(BaseModelForm, self).__init__(*args, **kwargs)
# if form has being submitted and
# model instance exists, then get data
if self.is_bound and self.instance.pk:
# get current model values
modeldict = model_to_dict(self.instance)
modeldict.update(self.data)
# add instance values to data
self.data = modeldict
But this should really be default behavior for DjangoModelFormMutation
.