0

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',
    )

enter image description here

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!

1

1 Answer 1

1

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,
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.