django表单为IntegerField添加空白选项

时间:2014-04-01 15:47:52

标签: django forms null

我正在尝试为表单选择添加“null”选项,因为该字段不是必需的。 现在对于char字段,我可以找一个空白并稍后解决,但这不适用于整数字段。

我的问题是,如何为CharField和IntegerField添加Null选项

我的选择:

LIGHT = 0
MEDIUM = 1
HEAVY = 2
ARMORTYPES = (
    (LIGHT, 'Light'),
    (MEDIUM, 'Medium'),
    (HEAVY, 'Heavy'),
)

我的模特:

class Item(models.Model):
    name = models.CharField(max_length=63, unique=True)
    flavor = models.TextField()
    price = models.IntegerField()
    weight = models.FloatField()
    armor_proficiency = models.IntegerField(choices=ARMORTYPES, null=True)
    (some more fields)

形式:

class ItemForm(forms.ModelForm):
    name = forms.CharField(help_text="name of the item")
    flavor = forms.CharField(widget = forms.Textarea, help_text="short description")
    price = forms.IntegerField(help_text="its market price")
    weight = forms.FloatField(help_text="weight of the item")

    armor_proficiency = forms.ChoiceField(choices=BLANK_CHOICE + ARMORTYPES, required=False, help_text="Some help text")

任何想法我怎么能解决这个问题?

0 个答案:

没有答案