代码之家  ›  专栏  ›  技术社区  ›  Mantas Vidutis

我有一个Django ModelForm,添加了字段和字段限制。并非所有字段都显示

  •  0
  • Mantas Vidutis  · 技术社区  · 13 年前

    我的模型形式:

    class CustomerBillingForm(ModelForm):
    
        expiration_month = models.IntegerField(choices=(1,2,3,4,5,6,7,8,9,10,11,12))
        expiration_year = models.IntegerField(max_length=4)
        card_number = models.IntegerField(max_length=16)
        card_cvv = models.IntegerField(max_length=4)
    
        class Meta:
            model = Customer
            fields = ['billing_first_name', 'billing_last_name', 'billing_address',
                      'billing_address_2', 'billing_city', 'billing_state',
                      'billing_zip', 'billing_country']
    

    当我在模板中使用此表单时,我不会得到4个自定义字段,只得到“字段”列表中的字段。我无法将前4个字段添加到字段列表中。我有什么选择?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Peter DeGlopper    13 年前

    使用正确的字段类-在这种情况下, forms.IntegerField 而不是 models.IntegerField 。您还需要更改参数,例如,覆盖 expiration_month 成为 select 小部件,对于那些试图设置max_length的小部件,您需要传递 maxlength 参数传递到小部件,并定义要使用的任何验证。

        2
  •  0
  •   HankMoody    13 年前

    如果您使用 fields 在里面 Meta 然后,您必须详细列出要使用的所有字段,即使是您正在添加到表单中但不在模型中的字段。

    你的意思是什么?你不能改变 领域 在里面 元 ? 你能换一下吗 __init__ 表单上的函数?您也可以将此表单子类化并重写 领域 或 __初始化__ .