繁体   English   中英

django modelform显示现有实例

[英]django modelform showing an existing instance

我有以下模型:

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

在这里, ConsentFormTemplateAbstractForm的子类。 我写yes_no_required的形式,因为对于nullbooleanfield的formfield空值将返回一个None ,这是我不想要的。

现在,当我想实例化表单以显示现有记录LiveConsentForm(instance=templateInstance) ,我遇到了问题。

我的templateInstance.yes_no_required的值为None,但是在呈现html时,选择了------ (当yes_no_requiredTrueFalse时,我没有这种麻烦)

在这里需要一些帮助。

好的,我已经看过forms.fields和

事实证明,当渲染为html时,结果将None仅仅视为'',并且在这里不考虑empty_value。 我不知道为什么@Ignacio巴斯克斯 - 艾布拉姆斯的家伙拿走了他的答案,所以如果可以请你把它放回去再升,我会接受它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM