步骤1:停用前端小部件
- 使用HTML
readonly
属性:http - //www.w3schools.com/tags/att_input_readonly.asp
或
disabled属性:http :
//www.w3.org/TR/html401/interact/forms.html#adef-
disabled
您可以通过小部件attrs属性注入任意HTML键值对:
myform.fields['status'].widget.attrs['readonly'] = True # text inputmyform.fields['status'].widget.attrs['disabled'] = True # radio / checkbox
步骤2:确保在后端有效禁用该字段
覆盖字段的干净方法,以便无论POST输入如何(有人可以伪造POST,编辑原始HTML等),您都可以获取已经存在的字段值。
def clean_status(self): # when field is cleaned, we always return the existing model field. return self.instance.status



