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

后端 未结 3 1162
感情败类
感情败类 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:25

    Combined @Daniels, @HardQuestions' and @jonespm answers:

    class MarkNewInstancesAsChangedModelForm(forms.ModelForm):
        def has_changed(self):
            """Returns True for new instances, calls super() for ones that exist in db.
            Prevents forms with defaults being recognized as empty/unchanged."""
            return not self.instance.pk or super().has_changed()
    

提交回复
热议问题