【发布时间】:2016-06-23 02:10:23
【问题描述】:
我很惊讶这很难做到。但是我想出了这个,这似乎至少适用于我的简单案例。谁能推荐一个更好的方法?
def field_changed(self, fieldname):
"""Tests if the value of the field changed from the original data"""
orig_value = self.fields[fieldname].initial or getattr(self.instance, field, None)
orig_value = getattr(orig_value, 'pk', orig_value)
if type(orig_value) is bool:
# because None and False can be interchangeable
return bool(self.data.get(fieldname)) != bool(orig_value)
else:
return unicode(self.data.get(fieldname)) != unicode(orig_value)
【问题讨论】:
标签: django django-forms