1

我已经阅读了有关枚举和 SQLAlchemy 的所有其他帖子,例如这篇文章,但是当我基于它们创建示例时,我得到了错误'None' has no attribute 'label'。以下代码中的错误是什么?我没有使用 Flask 或 SQLAlchemy 的经​​验,所以它可能是微不足道的。

我还尝试使用自定义的 ArrayOfEnum 类和create_constraint=False, native_enum=False参数,但这并没有帮助,而且我的数据库中有真正的枚举需要支持。

当我只使用枚举时,当我执行 Flask-AppBuilderflask run并打开http://127.0.0.1:5000/softwareproductview/add. 但是,当使用这样的枚举数组时,它总是会崩溃:

from flask_appbuilder import Model, Base
from enum import Enum
from sqlalchemy import Column, String
import sqlalchemy.dialects.postgresql as pg
import sqlalchemy as sa

class Softwareproduct(Model):
    suffix = Column(String(200), primary_key=True)
    # jinja2.exceptions.UndefinedError: 'None' has no attribute 'label'
    databasesystems =  Column(pg.ARRAY(sa.Enum("MySql","PostgreSql")))                                                                                                                                                                                                                                                        
    #databasesystems = Column(sa.Enum("MySql","PostgreSql")) # works

完全错误

2020-08-31 18:01:48,646:ERROR:app:Exception on /softwareproductview/add [GET]
Traceback (most recent call last):
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/security/decorators.py", line 109, in wraps
    return f(self, *args, **kwargs)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/views.py", line 588, in add
    return self.render_template(
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/baseviews.py", line 280, in render_template
    return render_template(
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/templating.py", line 137, in render_template
    return _render(
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/templating.py", line 120, in _render
    rv = template.render(context)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/general/model/add.html", line 2, in top-level template code
    {% import 'appbuilder/general/lib.html' as lib %}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/base.html", line 1, in top-level template code
    {% extends base_template %}
  File "/home/konrad/projekte/hito/database-frontend/app/templates/base.html", line 1, in top-level template code
    {% extends 'appbuilder/baselayout.html' %}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/baselayout.html", line 2, in top-level template code
    {% import 'appbuilder/baselib.html' as baselib %}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/init.html", line 46, in top-level template code
    {% block body %}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/baselayout.html", line 19, in block "body"
    {% block content %}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/general/model/add.html", line 7, in block "content"
    {% block add_form %}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/general/model/add.html", line 8, in block "add_form"
    {{ widgets.get('add')(form_action=form_action)|safe }}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/widgets.py", line 37, in __call__
    return template.render(args)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/general/widgets/form.html", line 45, in top-level template code
    {{ lib.render_field(field, begin_sep_label, end_sep_label, begin_sep_field, end_sep_field) }}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/runtime.py", line 679, in _invoke
    rv = self._func(*arguments)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_appbuilder/templates/appbuilder/general/lib.html", line 230, in template
    {{ field.label.text }}
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/jinja2/environment.py", line 471, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'label'
4

0 回答 0