Python 显示现有实例的django modelform
我有以下型号:Python 显示现有实例的django modelform,python,django,django-forms,Python,Django,Django Forms,我有以下型号: ynChoice = ((None, 'Acknowledgement not required'), (True, 'Acknowledgement and consent required'), (False, 'Acknowledgement required but consent not required')) class AbstractForm(models.Model): """base class for
ynChoice = ((None, 'Acknowledgement not required'),
(True, 'Acknowledgement and consent required'),
(False, 'Acknowledgement required but consent not required'))
class AbstractForm(models.Model):
"""base class for the below 2 classes"""
#require users to say yes and no other than acknowledging #null = not required
yes_no_required = models.NullBooleanField(choices=ynChoice, blank=False)
我将其映射到我的模型表单:
class LiveConsentForm(forms.ModelForm):
ynChoice = (
('', '---------------'),
(None, 'Acknowledgement not required'),
(True, 'Acknowledgement and consent required'),
(False, 'Acknowledgement required but consent not required'),
)
yes_no_required = forms.TypedChoiceField(choices=ynChoice,empty_value='')
class Meta:
model = ConsentFormTemplate
这里,approvementformtemplate
是AbstractForm
的一个子类。我在表单中写入了yes\u no\u required
,因为nullbooleanfield的formfield的空值将返回一个None
,这是我不想要的
现在,当我想实例化我的表单以显示现有记录时,liveapproverform(instance=templateInstance)
,我遇到了一个问题
MytemplateInstance.yes\u no\u required
的值为None,但在呈现html时,----
被选中。(当需要的是
为真
或为假
时,我不会遇到这种问题)
这里需要一些帮助。好的,我已经查看了forms.fields和
结果表明,在呈现为html时,没有一个被视为“”,此处不考虑空的_值。我不知道为什么@Ignacio Vazquez Abrams dude拿走了他的答案,所以如果你能把它放回去,我会接受的