本文介绍了如何使用Flask-Restless在日期搜索模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,搜索查询应该看起来像如:



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

?我试过:

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



即使有一些日期在2015年1月20日之前,也没有结果。我试过:

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





这是用户模型:
所有结果都返回,即使是2015年1月20日之前的结果。

  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)
密码= db.Column(db.String(255))
active = db.Column(db.Boolean())
activated_at = db.Column(db.DateTime())
角色= db.relationship('Role',secondary = roles_users,
backref = db.backref('users',lazy ='dynamic'))

查询日期的正确方法是什么?

解决方案

Davidism is绝对是对的,我不应该说JSON datetime-- JS ON本身没有一种表示日期/日期时间格式的方法 - 它在通信的发送者/接收者端识别嘿,我期待在这里约会,这看起来像一个日期,我会尝试和对待它就像一个。一般来说,大多数人使用ISO 8601作为他们在JSON的日期时间表示( 2014-10-23T18:25:43.511 Z ),Flask-Restless似乎使用 dateutil.parse 来将一些传入值解析回日期时间字段中, - 但奇怪的是,不是GET的。

所以看起来好像表示日期时间值的字符串需要与数据库期望的值相匹配 - 平均来说, ISO 8601格式。

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 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.

这篇关于如何使用Flask-Restless在日期搜索模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:54