Best way to "pre-clean" a field before calling form.is_valid()?

I’m using the AutoNumeric js library to format some of my input fields on my html page, for example, adding a % after a numeric value. The problem is that gets posted back as a string but the model is expecting an integer. So the normal clean_<field_name> method never gets called since it deals with Python objects (the integer).

So I guess I have two options: either clean the fields in the page before the post happens or in the view before I call form.is_valid. Neither solution is pretty as I have several dozen fields. Do-able though, but are there any other options or is there something in Django that can handle this? I could change my model fields to be strings but I don’t want to do that, that would introduce all kinds of other issues.

Welcome @dave !

See the to_python method for form fields.

1 Like

I’ll try that. Thanks!