【问题标题】:domain openerp how to compare 2 date fields in odoo域 openerp 如何比较 odoo 中的 2 个日期字段
【发布时间】:2023-03-29 19:07:01
【问题描述】:

当我将收到产品的日期和数量更改为 stock.move 的日期时,我创建了一个方法,我在课堂上声明 start_date = fields.Datetime()

def onchange_project(self, cr, uid,start_date):
    """
    onchange handler of start date.
    """  
    pool_stockmove =self.pool.get('stock.move')
    domain =[('date','>=',start_date)]  
    ids = pool_stockmove.search(cr, uid, domain)

这种方法效果很好,但我想比较 start_date date >= start_date AND date

cr.execute('SELECT id \
                FROM hr_timesheet_sheet_sheet \
                WHERE (date_from <= %s and %s <= date_to) \
                    AND user_id=%s \
                    AND id <> %s',(sheet.date_to, sheet.date_from, new_user_id, sheet.id))
            if cr.fetchall():
                return False

谢谢

【问题讨论】:

    标签: openerp odoo openerp-7 openerp-8 odoo-8


    【解决方案1】:

    日期以字符串格式存储。您可以像在您的 hr 示例中那样使用 sql 进行比较,但我建议使用 ORM 进行比较,而不是原始 sql。它更方便,建议始终使用它,因为sql 会转义代码中编写的安全性和其他检查(但您的代码中可能不是这种情况)。

    很难理解您想比较哪个日期以及为什么不能比较。

    我猜你想做这样的事情:

    [('date', '>=', 'start_date'), ('date', '<=', 'start_date')]
    

    域被定义为元组列表。这样的语法意味着and 默认在元组之间使用。你可以这样指定(但它是一样的):

    ['&', ('date', '>=', 'start_date'), ('date', '<=', 'start_date')]
    

    与上面一行的意思相同(如果使用'|',则表示or)。

    Odoo damain 使用波兰符号:https://en.wikipedia.org/wiki/Polish_notation

    对于日期格式,如果需要,您可以使用 python 模块datetime 更改日期格式。

    【讨论】:

      【解决方案2】:

      你可以试试这样的

       cr.execute("SELECT id FROM hr_timesheet_sheet_sheet WHERE (date_from >= %s AND date_to <= %s) AND (user_id=%s) AND (id <> %s)",(sheet.date_from,sheet.date_to, new_user_id, sheet.id))                 
       if cr.fetchall():
           return False
      

      我希望这对你有帮助:)

      【讨论】:

      • 感谢您的回复。我不知道如何使用方法格式化日期以及如何调用它以使用 start_date 进行格式化。这正是我所需要的。
      【解决方案3】:

      这是一个简单的例子: employees = self.env['hr.employee'].search([], order='name asc')

          for employee in employees:
              presence_count = self.env['hr.attendance'].search_count([
                  ('employee_id', '=', employee.id),
                  ('check_in', '>=', date_start_obj.strftime(DATETIME_FORMAT)),
                  ('check_out', '<=', date_end_obj.strftime(DATETIME_FORMAT)),
              ])
      
              absence_count = date_diff - presence_count
      
              docs.append({
                  'employee': employee.name,
                  'presence': presence_count,
                  'absence': absence_count,
              })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 2018-12-25
        相关资源
        最近更新 更多