graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

Partial updates with DjangoModelFormMutation

Open dan-klasson opened this issue 6 years ago • 5 comments

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.

dan-klasson avatar Aug 01 '19 10:08 dan-klasson

Should I use this BaseModelForm insted of ModelForm?

soumita018 avatar Aug 23 '19 13:08 soumita018

@soumita018

Yes unless you want your data to be overwritten. I would recommend you use serializers instead though.

This is a really critical bug, unless I'm not totally mistaken.

dan-klasson avatar Aug 23 '19 14:08 dan-klasson

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 21 '19 14:11 stale[bot]

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Aug 27 '20 00:08 stale[bot]

Considering the project is dead now, I'm removing myself as assignee. I wouldn't recommend expecting a fix.

ulgens avatar May 09 '25 08:05 ulgens