Flask搜索查询不使用过滤器

时间:2016-01-22 08:42:45

标签: python flask flask-sqlalchemy

我有一个正在运行的Flask API,现在我想实现搜索查询。 我的理解是过滤器应用于客户端,Flask API负责处理它。

Flask==0.10.1
Flask-HTTPAuth==2.7.0
Flask-Limiter==0.9.1
Flask-Login==0.3.2
Flask-Mail==0.9.1
Flask-Principal==0.4.0
Flask-Restless==0.17.0

我已按照文档操作并执行了搜索查询,但仍然只是检索相同的结果:

http://flask-restless.readthedocs.org/en/latest/searchformat.html

没有过滤器:

curl -u aaa:bbb -H "Content-Type: application/json" http://0.0.0.0:8080/api/1.0/job/

{
  "jobs": [
    {
      "description": "ESXi job completed", 
      "reference": "07FC78BCC0", 
      "status": 1
    }, 
    {
      "description": "Server discovery failed. Please verify HTTPS/SSH parameters", 
      "reference": "A6EE28F4C0", 
      "status": -1
    }]
}

根据以下内容搜索查询: http://flask-restless.readthedocs.org/en/latest/searchformat.html

curl -u aaa:bbb -G -H "Content-Type: application/json" -d '{
> "filters": [{"name": "description", "op": "like", "val": "%ESXi%"}]}' http://0.0.0.0:8080/api/1.0/job/

或者

curl -u aaa:bbb -G -H "Content-Type: application/json" -d '{"filters": [{"name": "status", "op": "eq", "val":0}]}' http://0.0.0.0:8080/api/1.0/job/

显示了相同的结果。

这是我的Flask端点:

def get_jobs():
    """

    :return:
    """
    try:
        log.info(request.remote_addr + ' ' + request.__repr__())
        jobs = Model.Job.query.order_by(desc(Model.Job.job_start)).limit(settings.items_per_page).all()

        # =========================================================
        # Get JOBS
        # =========================================================

        values = ['description', 'status', 'reference']
        response = [{value: getattr(d, value) for value in values} for d in jobs]
        return jsonify(jobs=response)


    except Exception, excpt:
        log.exception(excpt.__repr__())
        response = json.dumps('Internal Server Error. Please try again later')
        resp = Response(response, status=500, mimetype='application/json')
        return resp

我的模特

class Job(db.Model, AutoSerialize, Serializer):
    """

    """
    __tablename__ = 'job'
    __public__ = ('status','description','reference','job_start','job_end')
    id = Column(Integer, primary_key=True, server_default=text("nextval('job_id_seq'::regclass)"))
    description = Column(String(200))
    reference = Column(String(50))
    job_start = Column(DateTime)
    job_end = Column(DateTime)
    fk_server = Column(ForeignKey(u'server.id'))
    owner_id = Column(ForeignKey(u'auth_user.id'))
    is_cluster = Column(Boolean)
    host_information = Column(String(1024))
    status = Column(Integer, nullable=False)
    owner = relationship(u'AuthUser')
    server = relationship(u'Server')

    def serialize(self):
        """

        :return:
        """

        d = Serializer.serialize(self)
        return d

我需要改变什么吗?

1 个答案:

答案 0 :(得分:1)

__public__作为Job属性可能会干扰过滤的工作方式。 Flask-Restless文档中有关于此的警告。