A_K
A_K

Reputation: 902

How to set a default to a forms.ChoiceField()?

I am trying to set a default selection for payment choices to be Stripe, but I keep getting an errorTypeError: __init__() got an unexpected keyword argument 'default'

Here is the forms.py

PAYMENT_CHOICES = (
    ('S', 'Stripe'),
    ('P', 'Paypal')
)

class CheckoutForm(forms.Form):
    payment_option = forms.ChoiceField(
        widget=forms.RadioSelect, choices=PAYMENT_CHOICES, default='Stripe')

Upvotes: 1

Views: 170

Answers (1)

Fedor Soldatkin
Fedor Soldatkin

Reputation: 1203

At first, form.*Field classes has no default attribute. However, it has initial attribute represents a value which will be displayed after rendering a page.

Also in PAYMENT_CHOICES you should use in your code first values of tuples since these tuples contain real value in first place and verbose name in second.

That is how working version of your code may looks:

PAYMENT_CHOICES = (
    ('S', 'Stripe'),
    ('P', 'Paypal')
)

class CheckoutForm(forms.Form):
    payment_option = forms.ChoiceField(
        widget=forms.RadioSelect,
        choices=PAYMENT_CHOICES,
        initial='S')

Upvotes: 3

Related Questions