[Answer]-Extra fields in Django ModelForms: I can see them, but cannot save them to the model. Why?

1👍

The ModelForm machinery probably only fills in the instance fields you specified in fields. The way I would approach this problem is by overriding ModelForm.save(). Something like this:

class LocationForm(ModelForm):
    ...
    def save(self, commit=True, **kwargs):
        # create the instance but don't commit it to the database
        location = super(LocationForm, self).save(commit=False, **kwargs)

        # now set your cleaned fields on the model instance
        location.cleanedStreetAddress = ...

        if commit: 
            location.save() 

        return location

See the documentation on save() for more information.

0👍

Only the fields named in the ModelForm.Meta.fields list are automatically assigned from cleaned_data, if I am not mistaken. Instead of putting your derived data into cleaned_data you should just set the directly on self.instance in your ModelForm.

Leave a comment