【问题标题】:Flask restless - A search filter for a relationship.property != val in case of one-to-many relationshipFlask restless - 在一对多关系的情况下,relationship.property != val 的搜索过滤器
【发布时间】:2015-09-01 08:07:50
【问题描述】:

我的 bookactions 表的 Flask-sqlalchemy 模型是

class Book(db.Model):

    __tablename__ = 'book'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    activity = db.relationship('Actions')

class Actions(db.Model):

    __tablename__ = 'actions'

    id = db.Column(db.Integer, primary_key=True)
    book_id = db.Column(db.Integer, db.ForeignKey('book.id'), nullable=False)
    action = db.Column(db.Enum(['requested', 'issued', 'opened', 'closed']), nullable=True)
    created_on = db.Column(db.DateTime, default=_get_date_time)

基本上它是一本书和动作之间的一对多关系。我正在使用flask-restless 作为 API。

我需要得到

所有尚未关闭的书籍

我在search queries下面试过了

q={
"filters": [
    {
      "name": "activity",
      "op": "any",
      "val": {
        "name": "action",
        "op": "not_equal_to",
        "val": "closed"
      }
    }
  ]
}

q={
  "filters": [
    {
      "name": "activity",
      "op": "any",
      "val": {
        "name": "action",
        "op": "not_in",
        "val": ["closed"]
      }
    }
  ]
}

但我得到了错误的结果

{
"num_results": 30,
"objects": [
{
  "activity": [
    {
      "action": "opened",
      "created_on": "2015-06-05T17:05:07",
      "id": 31
    },
    {
      "action": "closed",
      "created_on": "2015-06-05T17:05:44",
      "id": 32
    }
  ],
  "id": 1,
  "name": "K&R"
  ....
},
...
]
"page": 1,
"total_pages": 3
}

我在这里犯了一些错误还是这种事情在不安分中是不可能的?

请帮帮我。

【问题讨论】:

    标签: flask flask-sqlalchemy flask-restless


    【解决方案1】:

    试试这个:

    q = { "filters":[   
             {
              "name":"activity__action", 
              "op":"not_equal_to", 
              "val":"closed"
             }
          ]
    }
    

    【讨论】:

    • 它给出了一个错误 - Can't compare a collection to an object or collection; use contains() to test for membership. with response = { "message": "Unable to construct query" }
    • 和 "op":"any", val:"opened" ? (flask-restless.readthedocs.org/en/0.7.0/searchformat.html的最后一段)
    • 但为此它执行类似SELECT * FROM book WHERE EXISTS (SELECT 1 FROM actions WHERE book.id = actions.book_id AND actions.action = %s) ORDER BY book.id ASC
    • 我想我想要类似SELECT * FROM book WHERE NOT EXISTS (SELECT 1 FROM actions WHERE book.id = actions.book_id AND actions.action = 'closed') ORDER BY book.id ASC
    • @Hussain 你找到解决方案了吗?
    猜你喜欢
    • 2015-05-24
    • 2015-10-14
    • 2013-01-10
    • 2011-06-14
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2016-08-23
    相关资源
    最近更新 更多