Frappe Database filters not working for datetime

Hello Everyone,

I’m trying to query a list of Events in the Event Doctype of the Core Module. It works when I pass a date but not when I pass a DateTime object. See my code below.

NOTE: All my Event Types are Public

What I’m trying to achieve is to return open events given a start datetime and end datetime constrain

Thanks a lot!

@frappe.whitelist()
def search_free_schedule(time_from, time_to):
    from_datetime_object = datetime.strptime(time_from, "%Y-%m-%d %H:%M:%S")
    to_datetime_object = datetime.strptime(time_to, "%Y-%m-%d %H:%M:%S")
    testing_return = []
    free_sched = frappe.db.get_list(
        "Event",
        # filters={"starts_on": [">=", time_from], "ends_on": ["<=", time_to]},
        filters=[["ends_on", "between", [time_from, time_to]]],
        )
    testing_return.append(free_sched)
    return testing_return

Hi @ranz,

Please try it. Perhaps, will solve your issue.

    free_sched = frappe.db.get_list(
        "Event",
        filters={"starts_on": [">=", from_datetime_object.strftime("%Y-%m-%d %H:%M:%S")],
        "ends_on": ["<=", to_datetime_object.strftime("%Y-%m-%d %H:%M:%S")]},
    )

Thank You!

1 Like

Hello @NCP, Thanks a lot!
I tried applying it, but it doesn’t work here are my screenshots

API REQUEST
Screen Shot 2023-06-03 at 5.58.30 PM

DOCType I’m Trying to QUERY

The Code I’m Trying to Execute