-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle Enum columns backed by Python Enums #164
base: master
Are you sure you want to change the base?
Conversation
With more thorough testing, I realised that since sa.Enum inherits from sa.String, the Length validator gets assigned to enum fields too, which leads to a crash when validating a form that contains enums, because they have no length. Now patched and added a unit test too.
And after the change:
|
With my above workarounds, rendering and validation no longer crash, but validation still fails for enum fields with "Not a valid choice" because the built-in wtforms_components SelectField does not like the trick of reversing values and labels: From
The exception gets raised, because (with my above example enum column) choices are I added a SelectField extension that overrides the choice_values creation to use enum instances instead of string values. |
In bfc25f8, support for Enums backing ChoiceType was added. But this doesn't cover native Enums directly backing the SQLAlchemy Enum column type (supported since SQLAlchemy v1.1).
For example, with an enum and an SQLAlchemy class that contains it like this:
My app crashes with a
ValueError: 'POWDER' is not a valid MaterialType
when attempting to render the form, as the default selected option contains an invalid value.I found several similar (but not the same) issues reported elsewhere:
I've added an additional case to handle this in WTForms-Alchemy > generator.py > select_field_kwargs.
Before the change:
After the change:
(There is also an irrelevant unit test failing in both cases, which I've reported in #163)