【问题标题】:How to get date range with frappe.db.get_list()?如何使用 frappe.db.get_list() 获取日期范围?
【发布时间】:2023-03-13 17:02:01
【问题描述】:

我正在尝试使用DB API documented here 获取在给定月份结束的订阅。

我可以在某个日期之前得到:

end_period = datetime.date(2020, 12, 31)
frappe.db.get_list('Subscription', filters={
    'current_invoice_end': ['<', end_period]
})

但是我如何在end_period 之前和start_period 之后指定呢?

当我尝试时

frappe.db.get_list('Subscription', filters={
    'current_invoice_end': ['<', end_period],
    'current_invoice_end': ['>', start_period]
})

它将其视为“或”并列出范围之外的内容。

交叉发布于discuss.erpnext.com

【问题讨论】:

    标签: erp frappe


    【解决方案1】:

    您可以在 erpnext src 中快速搜索“between”来检查实现。 对我来说,这是唯一可靠的来源。

    "holiday_date": ["between", (self.start_date, self.end_date)],
    

    您发布的解决方案不起作用,因为 Python 不允许在字典上使用两个具有相同名称的键。

    另一个返回列表的解决方案可能是

    holidays = frappe.db.sql_list('''select holiday_date, rate from `tabHoliday`
            where
                parent=%(holiday_list)s
                and holiday_date >= %(start_date)s
                and holiday_date <= %(end_date)s''', {
                    "holiday_list": holiday_list,
                    "start_date": self.start_date,
                    "end_date": self.end_date
                })
    

    【讨论】:

      【解决方案2】:

      您也可以将过滤器作为列表传递:

      frappe.db.get_list('Subscription', filters=[
          ['current_invoice_end', '<', end_period],
          ['current_invoice_end', '>', start_period]
      ])
      

      我会避免为此使用直接 SQL!

      【讨论】:

        猜你喜欢
        • 2016-10-06
        • 1970-01-01
        • 2021-06-22
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        相关资源
        最近更新 更多