我正在尝试在ModelForms中使用ChoiceField,下面是我在forms.py中的代码
Format_Choices=[('Line Numbering','Line Numbering'),('Header Numbering','Header Numbering')]
class EstimateForm(forms.ModelForm):
class Meta:
model=Estimate
estimateFormat=forms.ChoiceField(choices=Format_Choices,widget=forms.RadioSelect())此表与models.py中的以下估算模型相链接
class Estimate(models.Model):
estimateFormat=models.CharField(max_length=25,default='Line Numbering')在模板中使用
{{form.estimateFormat| as_crispy_field}}它会生成以下错误
|as_crispy_field got passed an invalid or inexistent field要使ChoiceField与models.py兼容,应该在models.py中使用哪个字段
发布于 2021-02-14 17:39:01
我们可以在CharField中添加选项,如下所示
class Estimate(models.Model):
EstimateFormat=( ('Line Numbering','Line Numbering'),
('Header Numbering','Header Numbering'),
)
estimateFormat=models.CharField(max_length=25, choices=EstimateFormat)ModelForms内幕
class EstimateModelForm(forms.ModelForm)
class Meta:
model=Estimate
fields='__all__'现在它可以工作了。
https://stackoverflow.com/questions/66194004
复制相似问题