Flask restless - A search filter for a relationship.property != val in case of one-to-many relationship

790 views Asked by At

Flask-sqlalchemy Models for my book and actions tables are

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)

Basically it's a one-to-many relationship between a book and actions. And I am using flask-restless for the API.

I need to get

All books that have not been closed

I have tried below search queries

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

and

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

But I am getting wrong result

{
"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
}

Am I making some mistake here or this kind of thing is not possible in restless?

Please help me out.

1

There are 1 answers

5
Meph- On

Try this:

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