75👍
✅
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
12👍
@daniel answer is great, however it will try to save the instance that is already created ever if no changes is made, which is not necessary, better to use it like:
class AlwaysChangedModelForm(ModelForm):
def has_changed(self, *args, **kwargs):
if self.instance.pk is None:
return True
return super(AlwaysChangedModelForm, self).has_changed(*args, **kwargs)
- [Django]-How are Django channels different than celery?
- [Django]-Problems extend change_form.html in django admin
- [Django]-Django multiple template inheritance – is this the right style?
4👍
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()
- [Django]-Django excel xlwt
- [Django]-How to create an empty queryset and to add objects manually in django
- [Django]-Auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'
Source:stackexchange.com