Django ModelForm RadioSelect Set Value or ID

452 Views Asked by At

I want to set the value of the RadioSelect field in my modelform:

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}

Right now, the generated code looks like this:

<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>

The values as they are are pretty much useless. My reward_denomination model has a slug field that would be perfect for the value (or better yet, as the id), but I'm unsure as to how I would set that myself. Any help?

0

There are 0 best solutions below