0

我的模型形式:

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 个字段添加到字段列表中。我有哪些选择?

4

2 回答 2

1

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

于 2013-08-26T21:48:38.927 回答
0

如果您使用fieldsinMeta那么您必须明确列出您要使用的所有字段,即使是您添加到表单中但不存在于模型中的字段。

You can't change the fieldsin是什么意思Meta?您可以更改__init__表单上的功能吗?您还可以子类化此表单并覆盖fieldsor __init__

于 2013-08-26T21:45:07.903 回答