django modelform显示现有实例

时间:2011-10-02 06:45:52

标签: 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 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的值为无,但是在呈现html时,会选择------。 (当yes_no_requiredTrueFalse时,我没有这样的麻烦

需要一些帮助。

1 个答案:

答案 0 :(得分:0)

好的,我看过forms.fields和

结果表明,当渲染到html时,结果是None只被视为'',并且这里不考虑empty_value。我不确定为什么@Ignacio Vazquez-Abrams老兄拿走了他的答案,所以如果你能再把它重新装回去,我会接受的。

相关问题