graphql-python / graphql-python/graphene-django

Partial updates with DjangoModelFormMutation

Open
#725 5 comments 3 reactions 0 assignees View on GitHub
🐛bug
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

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:

```py
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`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.