如何 运行 Flask-restless api 过滤查询 JSONB 内容
How to run a Flask-restless api filter query on JSONB content
我在尝试对 JSONB 对象中包含的数据执行 flask-restless 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 字段吗?
此时的答案是,您不能按原样使用 flask-restless。
我对 flask-restless 进行了一些修改,以拆分特殊格式的查询字符串并为 jsonb 正确转换查询。有点痛苦,而且可能无法扩展。
标记为在 flask-restless 上进行增强 git。
我在尝试对 JSONB 对象中包含的数据执行 flask-restless 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 字段吗?
此时的答案是,您不能按原样使用 flask-restless。
我对 flask-restless 进行了一些修改,以拆分特殊格式的查询字符串并为 jsonb 正确转换查询。有点痛苦,而且可能无法扩展。
标记为在 flask-restless 上进行增强 git。