You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<form method="post">
{{ form.hidden_tag() }}
{% for field in form %}
{% if field.widget.input_type != 'hidden' %} {{ field.label }} {% endif %}
{{ field }}
{% endfor %}
<input type="submit" value="Save">
<input type="reset" value="Reset">
The Form will show perfectly using the above template when selecting a pre-existing DB entry by ID. If I try to "Edit" that entry by simply clicking "Save" I get the following traceback.
adminui | Traceback (most recent call last):
adminui | File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2528, in wsgi_app
adminui | response = self.full_dispatch_request()
adminui | File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1825, in full_dispatch_request
adminui | rv = self.handle_user_exception(e)
adminui | File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1823, in full_dispatch_request
adminui | rv = self.dispatch_request()
adminui | File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1799, in dispatch_request
adminui | return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
adminui | File "/src/src/app.py", line 246, in clearancedetail
adminui | if form.validate():
adminui | File "/usr/local/lib/python3.8/site-packages/wtforms/form.py", line 329, in validate
adminui | return super().validate(extra)
adminui | File "/usr/local/lib/python3.8/site-packages/wtforms/form.py", line 146, in validate
adminui | if not field.validate(self, extra):
adminui | File "/usr/local/lib/python3.8/site-packages/wtforms/fields/core.py", line 242, in validate
adminui | stop_validation = self._run_validation_chain(form, chain)
adminui | File "/usr/local/lib/python3.8/site-packages/wtforms/fields/core.py", line 262, in _run_validation_chain
adminui | validator(form, self)
adminui | File "/usr/local/lib/python3.8/site-packages/wtforms/validators.py", line 138, in __call__
adminui | length = field.data and len(field.data) or 0
adminui | TypeError: object of type 'Category' has no len()
I added a len() method to the Enum object and that cleared the Traceback but then I get a data validation error when calling form.validate().
This works but I want my Enum definitions stored in a central place and not in the middle of my Model definition:
Once I defined enum_field_options() I had to manually add the field using SelectField. category = SelectField("Category", **enum_field_options(Category))
Using Flask, SQLAlchemy, WTForms against an MySQL DB.
Imports
Have an Enumerated Type Field that's stored in MySQL as a native Enum Column
Model Definition
Setting up Globals
Use ModelForm to Auto-Build Form
Flask Handler Snippet
The Form in the HTML Template
The Form will show perfectly using the above template when selecting a pre-existing DB entry by ID. If I try to "Edit" that entry by simply clicking "Save" I get the following traceback.
I added a len() method to the Enum object and that cleared the Traceback but then I get a data validation error when calling form.validate().
This works but I want my Enum definitions stored in a central place and not in the middle of my Model definition:
The text was updated successfully, but these errors were encountered: