Adding value to timestamp value to datetime object and applying condition to peewee where()

Skip to first unread message

Ramesh Pradhan

May 5, 2021, 8:23:22 AM5/5/21
to peewee-orm
I have model as follows:
class BlogModel(BaseModel):
    class Meta:
        table_name = "blogs"

    updated_at = DateTimeTZField(default=datetime.datetime.utcnow)
    frequency = CharField(choices=REPORT_FREQUENCY, default="once")
    status = CharField(default="active")
    deleted_at = DateTimeTZField(null=True)

My try (but get error):

now =

scheduled_reports =
BlogModel.status == "active",
BlogModel.frequency != "once",
BlogModel.updated_at.cast(int) +  get_timestamp_from_frequency(BlogModel.frequency)) > now,

I want to add updated_at with timestamp value and check if it is greater than now value.

Is there any specific way to query in single database hit? I know I can iterate over the rows and filter out the result using python after fetching from db. But I want to filter before fetching any row from database

Charles Leifer

May 5, 2021, 9:08:34 AM5/5/21
Yes, you can filter. I'd suggest consulting your database-specific documentation for date/time functions. I'm not sure that casting a DATETIME column works as you expect (additionally you would use .cast('INT')).

freq = get_timestamp...
.where(updated_at + SQL("interval '%s seconds'", freq) > now)

You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages