问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/27872133/how-to-search-a-model-by-date-with-flask-restless