-5

I am trying to use requests to send a filter query to Flask-Restless. The results are not filtered. How do I correctly write a filter?

import requests
import json
q = {'filters': [{'task': 'build an API', 'task': 'profit'}]}
r = requests.get('http://127.0.0.1:5000/toworks', params={'q': json.dumps(q)})

I expect the results to only contain the first and third items, but the second one is present as well.

{
    "towork1": {
        "task": "build an API"
    }, 
    "towork2": {
        "task": "?????"
    }, 
    "towork3": {
        "task": "profit!"
    }
}
davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

1

The syntax you've used for the filter is completely wrong. There should be a list of filters, each one being a dict with name, op, and value keys. Since you want to search within the value of a task, use the like op with wildcards (%) before and after the value you're filtering on. You'll also need to use an or since the two filters are mutually exclusive.

q = {'filters': [{'or': [
    {'name': 'task', 'op': 'like', 'val': '%build%'},
    {'name': 'task', 'op': 'like', 'val': '%profit%'}
]}]}
davidism
  • 121,510
  • 29
  • 395
  • 339