使用JSON在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)



慕田峪9158850
浏览 149回答 1
1回答

PIPIONE

好像Flask-AppBuilder缺乏对SQLAlchemy的Postgresql JSONB类型实现的支持。另一方面,要想想一个通用输入窗口小部件,而不是文本字段,它具有像JSON这样的灵活格式,这是具有挑战性的。追溯显示该异常在呈现搜索小部件的过程中引发,并且如果您检查调试器中的字段,那么可以肯定的是,该异常'properties'丢失了:[console ready]>>> self._fieldscollections.OrderedDict({'name': <wtforms.fields.core.StringField object at 0x7febfe492668>, 'csrf_token': <wtforms.csrf.core.CSRFTokenField object at 0x7febfe492978>})>>>&nbsp;一种“解决方案”是将有问题的字段完全从表单中排除:class PView(ModelView):&nbsp; &nbsp; datamodel = SQLAInterface(Profile)&nbsp; &nbsp; list_columns = ['id', 'name', 'properties']&nbsp; &nbsp; add_exclude_columns = ['properties']&nbsp; &nbsp; edit_exclude_columns = ['properties']&nbsp; &nbsp; search_exclude_columns = ['properties']当然,从根本上无法使用属性的意义上说,这根本不是解决方案。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python