在 admin 中
form.changed_data 是一个列表,存有真实改变的字段名称
def save_model(self, request, obj, form, change):
if change and form.changed_data:
print('change!!!')
else:
print('no change!!!')
super(ScenicAdmin, self).save_model(request, obj, form, change)
在 model 中
class MyModel(models.Model):
f1 = models.CharField(max_length=1)
def save(self, *args, **kw):
if self.pk is not None:
orig = MyModel.objects.get(pk=self.pk)
if orig.f1 != self.f1:
print 'f1 changed'
super(MyModel, self).save(*args, **kw)
在 form 中
class MyModelForm(forms.ModelForm):
def clean(self):
cleaned_data = super(ProjectForm, self).clean()
#if self.has_changed(): # new instance or existing updated (form has data to save)
if self.instance.pk is not None: # new instance only
if self.instance.f1 != cleaned_data['f1']:
print 'f1 changed'
return cleaned_data
class Meta:
model = MyModel
exclude = []