如何对 JSONB 内容运行 Flask-restless api 过滤器查询
Posted
技术标签:
【中文标题】如何对 JSONB 内容运行 Flask-restless api 过滤器查询【英文标题】:How to run a Flask-restless api filter query on JSONB content 【发布时间】:2017-05-14 02:46:18 【问题描述】:我在尝试对 JSONB 对象中包含的数据执行无烧瓶 api 查询时遇到问题。我猜是因为底层查询不知道如何转换数据字段,所以它不知道如何正确生成查询,但我不确定。
执行 ajax 搜索时运行的查询:
http://192.168.163.129:1080/api/subscribers?page=1&results_per_page=10&q="filters":["name":"data","op":"has","val":"name":"full_name","op":"eq","val":"%Steve%"],"order_by":["field":"created","direction":"asc"]
这是我运行查询时得到的回溯。
2016-12-29 22:26:33,068 - app - ERROR - mapper
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask_restless/views.py", line 1172, in _search
result = search(self.session, self.model, search_params)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 587, in search
query = create_query(session, model, search_params, _ignore_order_by)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 549, in create_query
_ignore_order_by)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 480, in create_query
filters = [create_filt(model, filt) for filt in search_params.filters]
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 438, in _create_filter
return create_op(model, fname, filt.operator, val, relation)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 411, in _create_operation
return opfunc(field, argument, fieldname)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 106, in <lambda>
'has': lambda f, a, fn: f.has(_sub_operator(f, a, fn)),
File "/usr/local/lib/python2.7/dist-packages/flask_restless/search.py", line 37, in _sub_operator
submodel = model.property.mapper.class_
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 833, in __getattr__
return self._fallback_getattr(key)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 811, in _fallback_getattr
raise AttributeError(key)
AttributeError: mapper
API 代码:
manager.create_api(Subscribers, methods=['GET'], results_per_page=10,
preprocessors=dict(GET_SINGLE=[subscribers_preprocessor],
GET_MANY=[subscribers_preprocessor]))
预处理器是一个简单的身份验证处理器。
知道如何正确格式化过滤器以过滤 JSONB postgres 字段吗?
【问题讨论】:
你能发布Subscribers
类的定义吗?
我认为这并不重要。但它是一个以 db.JSONB 而不是 db.String 作为标识符的通用表。
【参考方案1】:
此时的答案是,你不能像以前那样使用 flask-restless。
我对 flask-restless 进行了一些修改,以拆分特殊格式的查询字符串并将查询正确转换为 jsonb。有点痛苦,而且可能无法扩展。
它被标记为在flask-restless git上增强。
https://github.com/jfinkels/flask-restless/issues/623
【讨论】:
以上是关于如何对 JSONB 内容运行 Flask-restless api 过滤器查询的主要内容,如果未能解决你的问题,请参考以下文章
PostgreSQL:如何对包括 JSONB 字段的属性求和,并保留表格形状?