某个字段修改,记录下修改人 修改前后的值, 现在我在 ModelForm clean 函数里处理了下,感觉 。。。。挺蛋疼的。
class SynthesisStepForm(BootstrapModelForm):
class Meta:
model = SynthesisStep
fields = ('title', 'version', 'condition', 'yield_rate', 'testing', 'stype')
widgets = {
'testing': forms.Textarea(attrs={'rows': '4', 'cols': '100', 'style': 'width:auto;'}),
'condition': forms.Textarea(attrs={'rows': '4', 'cols': '100', 'style': 'width:auto;'})
}
def clean(self):
"""
用于监控字段被修改
:return:
"""
update_fields = ('title', 'version', 'condition', 'yield_rate', 'testing', 'stype')
cleaned_data = super(BootstrapModelForm, self).clean()
update_note = supervisory_update_fields(self, cleaned_data, update_fields)
self._update_note = update_note
return cleaned_data
def supervisory_update_fields(form_obj, cleaned_data, update_fields):
"""
用于监控 form 提交字段的变化
:param form_obj:
:param cleaned_data:
:param update_fields:
:return:
"""
update_note = ''
if form_obj.instance.pk: # new instance only
changed_data = form_obj.changed_data
for data in changed_data:
if data in update_fields:
try:
old_field = eval('form_obj.instance.{0}'.format(data))
new_field = cleaned_data[data]
if old_field != new_field:
update_note = update_note + '{2} is modified , from "{0}" to "{1}"\n'.format(old_field,new_field, data)
except Exception as e:
pass
return update_note
1
izoabr 2019-07-29 15:49:26 +08:00
内容版本管理,我记得有好几个现成的,刚搜了一下:
AuditTrail django-reversion |
2
noobsheldon 2019-07-29 16:20:22 +08:00 via Android
django signal
|
3
37Y37 2019-07-29 18:49:26 +08:00
signals,参考文档:
https://ops-coffee.cn/s/cMxdAfsTno56ixurmD4KXA |