djangodjango-modelsdjango-formsdjango-inheritance

Conflict in form save() method when inherit from other save() method in Django


I changed the save method in the Django form.Then I inherited another save method from this method and made some changes to the child method ,that conflicted. I can't figure out how to fix the conflict so that my other uses of the parent method stay healthy and don't get spoiled.

Forms.py

class BaseModelForm(forms.ModelForm):
    def save(self, commit=True, **kwargs):
        """
        Save this form's self.instance object if commit=True. Otherwise, add
        a save_m2m() method to the form which can be called after the instance
        is saved manually at a later time. Return the model instance.
        """
        if self.errors:
            raise ValueError(
                "The %s could not be %s because the data didn't validate." % (
                    self.instance._meta.object_name,
                    'created' if self.instance._state.adding else 'changed',
                )
            )
        if commit:
            # If committing, save the instance and the m2m data immediately.
            self.instance.save(user=kwargs.pop('user'))
            self._save_m2m()
        else:
            # If not committing, add a method to the form to allow deferred
            # saving of m2m data.
            self.save_m2m = self._save_m2m
        return self.instance
class ChildForm(BaseModelForm):
    def save(self, commit=True, **kwargs):
        new_instance = super(ChildForm, self).save(commit=True)
        # Some other codes goes here!
        return new_instance

Models.py

class BaseFieldsModel(models.Model):
    def save(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        if user:
            if self.pk is None:
                self.created_by = user
            self.updated_by = user
        super(BaseFieldsModel, self).save(*args, **kwargs)

Views.py

def my_view(request,id):
        if form.is_valid():
            instance = form.save(commit=False)
            # Some codes goes here!
            instance.save(user=request.user)

And error is:

KeyError at /my/url
        Request Method: POST
        'user'
        Exception Type: KeyError
        Exception Value:    
        'user'
And Django Debug page separately highlight these three lines: 
        instance = form.save(commit=False) 
        new_instance = super(ChildForm, self).save(commit=True)
        self.instance.save(user=kwargs.pop('user'))

Solution

  • You're trying to get user in BaseModelForm.save(), but you never passed the user to the form.save() calls. You need to add form.save(..., user=request.user):

    def my_view(request,id):
        ...
        instance = form.save(commit=False, user=request.user)
    

    and also pass it along in super(ChildForm, self).save(..., **kwargs)

    class ChildForm(BaseModelForm):
        def save(self, commit=True, **kwargs):
            new_instance = super(ChildForm, self).save(commit=True, **kwargs)
            ...
    

    Also, you probably want to pass super(ChildForm, self).save(commit=commit, ...) in ChildForm:

    new_instance = super(ChildForm, self).save(commit=True, **kwargs)
    

    because otherwise the form class may not respect the commit flag being passed from the view (unless of course, you've already handled this in your elided code).