Cannot compare date-time with smallest and average value-last_seen

I want to update the last seen user column. For this I am trying to use this user model:

class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) ... last_seen = db.Column(db.DateTime(timezone=True), default=datetime.datetime.utcnow) def ping(self): self.last_seen = datetime.datetime.utcnow() db.session.add(self) db.session.commit() 

And this code, which is always executed when the user performs an action.

 @mod.before_app_request def before_request(): current_user.ping() 

This is mistake:

 TypeError: can't compare offset-naive and offset-aware datetimes 

How can i solve this? I am using postgres and the problem is easily modeled with the code I am showing.

+7
python sql flask datetime postgresql
source share
1 answer

Create a knowing date and time (the time that the time zone has):

 import pytz self.last_seen = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) 

In this case, you will want to create an aware time-time with the current time in UTC.

To do this, you will need the pytz package (this package contains the latest time zone information and this information is not part of the Python Standard Library).

+10
source share

All Articles