将django表单中的外键字段设置为只读,并仍然允许提交表单

时间:2021-11-05 08:16:11

How do I make a foreign key field in a form read only but still allow this field to be recognized as valid once the form is submitted? According to W3C, disabled fields are left out once the form is submitted....using the code below, I can set the field as disabled, thus readonly, but my form doesn't go through

如何将表单中的外键字段设置为只读,但仍然允许在提交表单后将此字段识别为有效字段?根据W3C,一旦提交表单就禁用禁用字段....使用下面的代码,我可以将字段设置为禁用,因此只读,但我的表单不通过

    def __init__(self, *args, **kwargs):
       super(IssuesForm, self).__init__(*args, **kwargs)
       self.fields['vehicle'].widget.attrs['readonly'] = True

Ideas....?

3 个解决方案

#1


2  

I don't know the Django or Python syntax, however, an input field of type="hidden" might be what you're looking for. If you wanted to still display the value using a disabled field, you could do that too, and rely on the hidden field for the actual value.

我不知道Django或Python语法,但是,类型=“hidden”的输入字段可能是您正在寻找的。如果您仍想使用禁用字段显示值,您也可以这样做,并依赖隐藏字段作为实际值。

#2


0  

Maybe I can try the hidden field...I knew this was a possibility, but I wanted to be sure there was no other way

也许我可以尝试隐藏的领域......我知道这是一种可能性,但我想确定没有别的办法

#3


0  

I've had this problem and I used JavaScript to solve

我有这个问题,我用JavaScript来解决

#1


2  

I don't know the Django or Python syntax, however, an input field of type="hidden" might be what you're looking for. If you wanted to still display the value using a disabled field, you could do that too, and rely on the hidden field for the actual value.

我不知道Django或Python语法,但是,类型=“hidden”的输入字段可能是您正在寻找的。如果您仍想使用禁用字段显示值,您也可以这样做,并依赖隐藏字段作为实际值。

#2


0  

Maybe I can try the hidden field...I knew this was a possibility, but I wanted to be sure there was no other way

也许我可以尝试隐藏的领域......我知道这是一种可能性,但我想确定没有别的办法

#3


0  

I've had this problem and I used JavaScript to solve

我有这个问题,我用JavaScript来解决