Skip to content
Advertisement

Flask_SQLAlchemy is claiming my value is not boolean?

I ran into the following error:

JavaScript

I tried as boolean input everything ranging from 0|1, FALSE|TRUE, False|True on my main route. I have also tried to put in the boolean values inbetween quotations. What am I doing wrong?

JavaScript

My base route being as follows

JavaScript

Advertisement

Answer

The problem you have is that the form submission is returning the selection value as a string – literally "True" or "False" – while the SQL driver expects a boolean type.

There is a Python standard library function distutils.util.strtobool which can safely convert a representation of a true or false value into a boolean type, raising a ValueError if someone puts something naughty into your API (this is much preferred to using eval() which shouldn’t be used on untrusted input).

I would update your route to something like the following:

JavaScript

One thing to note with strtobool is that distutils is now deprecated as of Python 3.10, and will be removed in 3.12. This answer shows the implementation of it as a function, which is quite trivial, so it’s worth including in your own utility functions for any code expected to last beyond Python 3.12.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement