Flask-SQLAlchemy select date is equal to today's data

ORM Model Tokens has a field that is the creation time, as follows:

 create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)

Screenshot in the database:
clipboard.png

stackoverflowcast:


db.cast(t.create_time, db.DATE)print:

clipboard.png

alone print (type (db.cast (t.createtimetime, db.DATE) , the result is < class" sqlalchemy.sql.elements.Cast" > , regardless of the type of result produced by db.cast , db.cast (t.createtimetime, db.DATE) = = db.cast (current_time.) Db.Date) should all the results of be True or False ?

Feb.27,2021

for t in Tokens.query.filter(Tokens.user_id == user_id).all():
    print(t.id, t.create_time.date() == current_time,now())

your create_time field itself is created through datetime.now (). Therefore, the data of the datetime object directly supports the date () function.


according to reason, regardless of the type of result produced by db.cast, db.cast (t.createtimeout, db.DATE) = = db.cast (current_time.) Should all the results of db.Date be True or False?

if sqlalchemy overloads the operator = = by implementing the magic method _ _ eq__, then the = = operation of these two objects does not necessarily return True/False
for example:

>>> class C(object):
    def __init__(self, name):
        self.name = name
    def __eq__(self, other):
        return '{}=={}'.format(self.name, other.name)

    
>>> a = C('jack')
>>> b = C('lucy')
>>> a == b
'jack==lucy'

has been resolved by using

Tokens.query.filter(Tokens.user_id == user_id,
    db.cast(Tokens.create_time, db.DATE) == db.cast(current_time, db.DATE)).all()

you can select data with today's date.
print (db.cast (Tokens.create_time, db.DATE) = = db.cast (current_time, db.DATE)) does not understand characters. Personal guess is that the built-in class modifies the _ _ repr__ method, including the use of print (type (db.cast (t.createroomtimetimedb.DATE) = = db.cast (current_time, db.Date) and the result is also < class' sqlalchemy.sql.elements.BinaryExpression' > . But it can be used as a normal Boolean value.

Menu