Search code examples
pythondjangodjango-formsformsetinline-formset

Django formset return empty instances


I have a class that inheirts from BaseInlineFormSet, where I override the save() method:

class CustomBaseModelFormSet(BaseInlineFormSet):
    def save(self, something=None, commit=True, *args, **kwargs):
        instances = super(CustomBaseModelFormSet, self).save(commit=False)

but when I call formset.save() to create new objects with a bound and valid formset, the variable instances is an empty list!

Why the the save() of the parent class is not saving? there's no trace of any error.

I'm using Python 3 and Django 1.10.5.


Solution

  • Turns out that in the BaseModelFormSet class, there's a save_new_objects() method that was skipping my forms to be saved due to the following check:

    if not form.has_changed():
        continue
    

    So, in my ModelForm I overwrote this method:

    def has_changed(self):
        return True
    

    This way it always return True and my forms are saved.