Django ModelForm RadioSelect设置值或ID

时间:2013-12-16 02:45:40

标签: python django modelform

我想在我的模型中设置RadioSelect字段的值:

class Redeem_Form(forms.ModelForm):
    quantity = forms.IntegerField(max_value=10, min_value=1, label=_('quantity'), initial=1, error_messages={'invalid':'This value must be between 1 and 10'})
    class Meta:
        model = Reward
    fields = ['reward_denomination']
        widgets = {'reward_denomination': forms.RadioSelect}

现在,生成的代码如下所示:

<input name="reward_denomination" type="radio" id="gift-card_0" value=1>
    <label for="gift-card_0">$5 Gift Card</label>

<input name="reward_denomination" type="radio" id="gift-card_1" value=2>
    <label for="gift-card_1">$10 Gift Card</label>

<input name="reward_denomination" type="radio" id="gift-card_2" value=3>
    <label for="gift-card_2">$20 Gift Card</label>

它们的价值几乎没用。我的reward_denomination模型有一个slug字段,对于值来说是完美的(或者更好,作为id),但我不确定我将如何自己设置它。有什么帮助吗?

0 个答案:

没有答案