如何比较 "peewee.DateField" 和 "datatime.date"?

How to compare "peewee.DateField" with "datatime.date"?

我写了下面的程序来获取我的数据库的一些行,其中包含有关 22-Jan-1963 之后出生的用户的信息:

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):

>>> ================================ RESTART ================================
>>> 
Bob 1960-01-21 False
James 1965-01-22 True
Steve 1970-01-20 True
>>> 

嗯,我在where()方法中用"22-Jan-1963"替换了dt.date(1963,1,22),现在结果是:

>>> ================================ RESTART ================================
>>> 
James 1965-01-22 True
>>> 

如你所见,还是不正确。

我该怎么办?

我完全不知道 PeeWee,但考虑到 Sqlite 没有本机日期时间格式(它将它模拟为字符串),您可能想尝试将日期格式更改为 "%Y-%m-%d" ;这将自动正确排序为字符串,然后可能适用于 Sqlite。

documentation之后,我建议

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
)

是的,很冗长...