使用JSON

时间:2018-08-14 13:44:06

标签: python sqlalchemy flask-appbuilder

尝试在数据库中为我的表创建此模型,但是在查看表时遇到此关键错误,然后又出现此错误,我怀疑这是将数据添加到表中的问题的链接,非常感谢您的帮助。我还在视图模块中创建了一个视图,但是它只是模型之间的简单连接,我列出了字段,我将包括appbuilder所需的视图,

views.py:

class PView(ModelView):
    datamodel = SQLAInterface(Profile)
    list_columns = ['id', 'name', 'properties']

appbuilder.add_view(PView, "X", icon="fa-envelope", category="X")

models.py:

class Profile(Model):
    __tablename__ = 'profile'
    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    properties = Column(JSONB, nullable=True)

    def __repr__(self):
        return self.name

跟踪:

Traceback (most recent call last):
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/security/decorators.py", line 26, in wraps
    return f(self, *args, **kwargs)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/views.py", line 478, in list
    widgets=widgets)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/baseviews.py", line 160, in render_template
    return render_template(template, **dict(list(kwargs.items()) + list(self.extra_args.items())))
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 2, in top-level template code
    {% import 'appbuilder/general/lib.html' as lib %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/base.html", line 1, in top-level template code
    {% extends base_template %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/baselayout.html", line 2, in top-level template code
    {% import 'appbuilder/baselib.html' as baselib %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/init.html", line 46, in top-level template code
    {% block body %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/baselayout.html", line 19, in block "body"
    {% block content %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 7, in block "content"
    {% block list_search scoped %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 8, in block "list_search"
    {% call lib.accordion_tag("accordion1",_("Search"), False) %}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/general/lib.html", line 254, in template
    {{ caller() }}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/templates/appbuilder/general/model/list.html", line 9, in template
    {{ widgets.get('search')()|safe }}
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/flask_appbuilder/widgets.py", line 107, in __call__
    label_columns[col] = as_unicode(self.template_args['form'][col].label.text)
  File "/Users/adam/myapp/venv/lib/python3.6/site-packages/wtforms/form.py", line 65, in __getitem__
    return self._fields[name]
KeyError: 'properties'

1 个答案:

答案 0 :(得分:0)

似乎Flask-AppBuilder不支持SQLAlchemy对Postgresql JSONB类型的实现。另一方面,想像一个通用输入窗口小部件以灵活的格式(例如JSON)(而不是文本字段)来实现具有挑战性。

回溯表明,在呈现搜索小部件时会引发异常,并且如果您检查调试器中的字段,那么可以肯定地发现'properties'是否缺失:

[console ready]
>>> self._fields
collections.OrderedDict({'name': <wtforms.fields.core.StringField object at 0x7febfe492668>, 'csrf_token': <wtforms.csrf.core.CSRFTokenField object at 0x7febfe492978>})
>>> 

“解决方案”是将有问题的字段完全从表单中排除:

class PView(ModelView):
    datamodel = SQLAInterface(Profile)
    list_columns = ['id', 'name', 'properties']
    add_exclude_columns = ['properties']
    edit_exclude_columns = ['properties']
    search_exclude_columns = ['properties']

从本质上说,这实际上不是一个解决方案,因为它使属性实际上无法使用。