I've got a form like this:
class SomeForm(forms.Form):
first = forms.IntegerField(max_value= DontWantToSetYet)
second = forms.IntegerField(max_value= DontWantToSetYet)
third = forms.IntegerField(max_value= DontWantToSetYet)
How can I set the max_values at runtime? I.E
if request.method == 'POST':
form = SomeForm(request.POST, max_values = {'first':10,'second':50,'third':666})
[...]
you can set the max values on fields in the __init__
method, as shown here
class SomeForm(forms.Form):
def __init__(self, max_values, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
self.fields['first'] = forms.IntegerField(max_value=max_values['first'])
...
first = forms.IntegerField(max_value= DontWantToSetYet)
second = forms.IntegerField(max_value= DontWantToSetYet)
third = forms.IntegerField(max_value= DontWantToSetYet)
edit:
self.fields['first'].max_value = max_values['first']
didn't worked when I tried, redefining the field definition like this works
self.fields['first'] = forms.IntegerField(max_value=max_values['first'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With