Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Modelform setting minimum Value on FloatField

i am trying to set the min_value Attribute on Form level within my modelform.

Forms.py

class ProductForm(forms.models.ModelForm):
    class Meta:
        model = Artikel
        localized_fields = '__all__'
        fields = ('price',)

Model.py

class Artikel(models.Model):
    price = models.FloatField(help_text ='Price')

How can i setup the modelform that i can constrain the values allowed on the modelform? I want the user to only enter values greater than or equal to 0.01. I dont want to restricted on Database Level cause i dont want to limit myself in that regard.

like image 770
Burnie800 Avatar asked Feb 22 '26 23:02

Burnie800


1 Answers

You can override the ModelForm's init method. This will set the min attribute on the field to 10:

    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)
        self.fields['price'].widget.attrs['min'] = 10
like image 119
voodoo-burger Avatar answered Feb 24 '26 16:02

voodoo-burger