How to force-save an “empty”/unchanged django admin inline?

后端 未结 3 1149
感情败类
感情败类 2020-12-05 09:42

I have some inlines in one of my admin models which have default values which likely won\'t need to be changed when adding a new instance with "Add another ...". U

3条回答
  •  有刺的猬
    2020-12-05 10:34

    It took me quite some time to figure out but it is actually really simple.

    from django.contrib import admin
    from django.forms.models import BaseInlineFormSet, ModelForm
    
    class AlwaysChangedModelForm(ModelForm):
        def has_changed(self):
            """ Should returns True if data differs from initial. 
            By always returning true even unchanged inlines will get validated and saved."""
            return True
    
    class CheckerInline(admin.StackedInline):
        """ Base class for checker inlines """
        extra = 0
        form = AlwaysChangedModelForm
    

提交回复
热议问题