Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i implement django input form with the default values

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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,
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading