[Answered ]-How to dynamically set the limit_value of the build-in MinValueValidator inside a Django 3.1 ModelForm

1👍

Below code examples show the answer to my question.

Model form class

class NewBidForm(forms.ModelForm):
    class Meta:
        model = Bid
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        my_arg = kwargs.pop('my_arg')
        super(NewBidForm, self).__init__(*args, **kwargs)
        self.fields['bid'].validators=[MinValueValidator(my_arg)]

Then every time a form object is instantiated make sure to pass in the my_variable like so:

form = NewBidForm(my_arg=my_variable)

My mistake was to instantiate the form at two locations in my code but only passing the argument in one of the instances.
I

Leave a comment