writing an online store I have a form to add items to cart with different quantities. I have implemented this through "TypedChoiceField":
class CartAddProductRetailForm(forms.Form):
quantity = forms.TypedChoiceField(
choices=PRODUCT_RETAIL_QUANTITY_CHOICES,
coerce=int,
label='quantity',
)
But it looks ridiculous.
Can you tell me how to implement it with a "CharField" or "DecimalField" with a default '1' value.
quantity = forms.DecimalField(max_value=999, min_value=1, )
Thanks!
CodePudding user response:
Use Form.initial
(docs).
#when instantiating your form
quantity = forms.DecimalField(max_value=999, min_value=1, initial={'quantity':1})
or
#when defining your form
class CartAddProductRetailForm(forms.Form):
quantity = forms.DecimalField(
choices=PRODUCT_RETAIL_QUANTITY_CHOICES,
coerce=int,
label='quantity',
initial=1,
)