如何使只读的形式的外键字段,但仍允许该领域,一旦表单被提交到被识别为有效? 根据W3C,残疾人领域被排斥在外,一旦提交表单....使用下面的代码,我可以设置该字段为禁用,因而是只读的,但我的形式不经过
def __init__(self, *args, **kwargs):
super(IssuesForm, self).__init__(*args, **kwargs)
self.fields['vehicle'].widget.attrs['readonly'] = True
想法....?
我不知道Django的或Python语法,但是,类型的输入栏=“隐藏”可能是你在找什么。 如果你想使用无效字段仍显示值,你可以做到这一点,靠实际值的隐藏字段。
也许我可以尝试隐藏字段......我知道这是有可能的,但我想,以确保没有其他办法
我碰到这个问题就来了很多其他的解决方案后,似乎没有工作。 下面是我如何得到它成功地利用了“隐藏”的建议,在情况下,它是有帮助的其他人的工作示例代码。
class EditExifForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EditExifForm, self).__init__(*args, **kwargs)
self.fields['image'].widget.attrs['hidden'] = True # This is the solution
# Setting as 'readonly' didn't make a difference
# Setting as 'disabled' made the form not update the database
class Meta:
model = exif
...
文章来源: Make a foreign key field in a django form read-only, and still enable the form to be submitted