Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
[AbstractConfigForm] Made clean_templates more generic
Browse files Browse the repository at this point in the history
Instead of hardcoding the data passed to the temporary instance
used for validation, pass a copy of the cleaned_data dictionary,
carefully removing the "templates" key (which contains m2m relationships).

This way the method won't fail if third party apps add more required
fields to their models.
  • Loading branch information
nemesifier committed Feb 22, 2017
1 parent 8a27a4c commit ca975b8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions django_netjsonconfig/base/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ class Meta:
class AbstractConfigForm(BaseForm):
def clean_templates(self):
config_model = self.Meta.model
templates = self.cleaned_data.get('templates', [])
# copy cleaned_data to avoid tampering with it
data = self.cleaned_data.copy()
templates = data.pop('templates', [])
if self.instance._state.adding:
# when adding self.instance is empty, we need to create a
# temporary instance that we'll use just for validation
config = config_model(name=self.data.get('name'),
backend=self.data.get('backend'),
config=self.data.get('config'))
config = config_model(**data)
else:
config = self.instance
if config.backend and templates:
Expand Down

0 comments on commit ca975b8

Please sign in to comment.