2

According to the documentation, a search query should look like:

GET /api/person?q={"filters":[{"name":"age","op":"ge","val":10}]}

How do I compare a date? I tried:

GET /api/person?q={"filters":[{"name":"date","op":"<=","val":"1/20/2015"}]}

That gets no results, even though there are some which have dates prior to 1/20/2015. I tried:

GET /api/person?q={"filters":[{"name":"date","op":">=","val":"1/20/2015"}]}

That gets all results back, even ones that are from before 1/20/2015.

This is the User model:

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime)
    type =db.Column(db.String(255))
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active= db.Column(db.Boolean())
    activated_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

What is the correct way to query a date?

davidism
  • 121,510
  • 29
  • 395
  • 339
DFenstermacher
  • 564
  • 1
  • 9
  • 23

1 Answers1

3

Davidism is absolutely right, I shouldn't have said JSON datetime-- JSON doesn't itself have a way of representing a date/datetime format— it's on the sender/reciever ends of the communication to recognise "Hey, I'm expecting a date here, this looks like a date, I'll try and treat it like one".

Generally, most people use ISO 8601 as their datetime representations in JSON (2014-10-23T18:25:43.511Z), and Flask-Restless seems to use dateutil.parse to parse some incoming values back into datetime fields-- but oddly, not GET ones.

So it looks as though the string representing your datetime value needs to match what your database expects-- which on average is going to be an ISO 8601 format.

Doobeh
  • 9,280
  • 39
  • 32
  • 2
    I submitted a pull-request to the project, so Flask-Restless would now parse the date-strings sent in as a query. https://github.com/jfinkels/flask-restless/pull/379 – Doobeh Jan 10 '15 at 19:47
  • Thank you so much for your help. Both with the query, and for the pull request. I just got back to read your full answer, and mark it as correct. Thank you again. – DFenstermacher Jan 11 '15 at 07:46
  • I'm very glad for this answer, thank you! Is there a way to not have it filter on time as well? I only want to find tuples on a specific date, time doesn't matter and I will not know it. At the moment I get 0 results if I do ```Task?q={"filters":[{"name":"taskDate","op":"eq","val":"2015-11-08"}]}``` but 1 result if I do ```Task?q={"filters":[{"name":"taskDate","op":"eq","val":"2015-11-08T21:41:43"}]}``` Do you know how to only filter on date, not bothering with time? – stinaq Nov 10 '15 at 21:08