首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用django模型的属性错误表单选择字段

使用django模型的属性错误表单选择字段
EN

Stack Overflow用户
提问于 2021-02-14 17:25:05
回答 1查看 24关注 0票数 0

我正在尝试在ModelForms中使用ChoiceField,下面是我在forms.py中的代码

代码语言:javascript
复制
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中的以下估算模型相链接

代码语言:javascript
复制
class Estimate(models.Model):
      estimateFormat=models.CharField(max_length=25,default='Line Numbering')

在模板中使用

代码语言:javascript
复制
{{form.estimateFormat| as_crispy_field}}

它会生成以下错误

代码语言:javascript
复制
|as_crispy_field got passed an invalid or inexistent field

要使ChoiceField与models.py兼容,应该在models.py中使用哪个字段

EN

回答 1

Stack Overflow用户

发布于 2021-02-14 17:39:01

我们可以在CharField中添加选项,如下所示

代码语言:javascript
复制
class Estimate(models.Model):
      EstimateFormat=( ('Line Numbering','Line Numbering'),
                       ('Header Numbering','Header Numbering'),
                     )
      
      estimateFormat=models.CharField(max_length=25, choices=EstimateFormat)

ModelForms内幕

代码语言:javascript
复制
class EstimateModelForm(forms.ModelForm)
      class Meta:
          model=Estimate
      fields='__all__'

现在它可以工作了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66194004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档