Python 如何比较;“peewee.DateField”;加上;数据时间.日期“;?

Python 如何比较;“peewee.DateField”;加上;数据时间.日期“;?,python,python-2.7,datetime,peewee,datefield,Python,Python 2.7,Datetime,Peewee,Datefield,我编写了下面的程序来获取数据库中的一些行,其中包含1963年1月22日以后出生的用户的信息: import datetime as dt import peewee as pw db = pw.SqliteDatabase('people.db') class Person(pw.Model): name = pw.CharField() birthday = pw.DateField(formats=['%d-%b-%Y']) class Meta:

我编写了下面的程序来获取数据库中的一些行,其中包含1963年1月22日以后出生的用户的信息:

import datetime as dt
import peewee as pw
db = pw.SqliteDatabase('people.db')

class Person(pw.Model):
    name = pw.CharField()
    birthday = pw.DateField(formats=['%d-%b-%Y'])

    class Meta:
        database = db # This model uses the "people.db" database.

db.create_tables([Person])

bob = Person(name = 'Bob', birthday = '21-Jan-1960')
james = Person(name = 'James', birthday = '22-Jan-1965')
steve = Person(name = 'Steve', birthday = '20-Jan-1970')
alex = Person(name = 'Alex', birthday = '18-Jan-1975')
bob.save()
james.save()
steve.save()
alex.save()

for item in Person.select().where(Person.birthday > dt.date(1963,1,22)):
    print item.name,item.birthday, item.birthday > dt.date(1963,1,22)
但当我运行此程序时,输出不是我所期望的(我期望输出中包含James、Steve和Alex):

我在
where()
方法中将
dt.date(1963,1,22)
替换为
“22-Jan-1963”
,现在结果是:

>>> ================================ RESTART ================================
>>> 
James 1965-01-22 True
>>> 
正如你在上面看到的,这仍然是不正确的


我该怎么办?

我绝对不知道PeeWee,但鉴于Sqlite没有本机日期时间格式(它将其模拟为字符串),您可能需要尝试将日期格式更改为
%Y-%m-%d”
;这将自动作为字符串正确排序,然后可以用于Sqlite

other = dt.date(1963, 1, 22)
Person.select().where(
    Person.birthday.year >= other.year
).where(
    Person.birthday.year > other.year
    | Person.birthday.month >= other.month
).where(
    Person.birthday.year > other.year
    | Person.birthday.month > other.month
    | Person.birthday.day > other.day
)

是的,非常详细…

是的,您应该使用Y-m-d格式来确保排序正确。
other = dt.date(1963, 1, 22)
Person.select().where(
    Person.birthday.year >= other.year
).where(
    Person.birthday.year > other.year
    | Person.birthday.month >= other.month
).where(
    Person.birthday.year > other.year
    | Person.birthday.month > other.month
    | Person.birthday.day > other.day
)