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

151 views
Skip to first unread message

Ramesh Pradhan

unread,
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 = datetime.now(tz.UTC)

scheduled_reports = BlogModel.select().where(
BlogModel.deleted_at.is_null(True),
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

unread,
May 5, 2021, 9:08:34 AM5/5/21
to peewe...@googlegroups.com
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 peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/9953d705-74fb-4593-b9be-88539b183eb7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages