【问题标题】:Odoo , Domain, filter date and monthOdoo,域,过滤日期和月份
【发布时间】:2015-08-17 10:17:36
【问题描述】:

我尝试在 sale.report 中创建一个过滤域:Month(order_date) = month(current_date) AND Day(order_date)

filter string="This Month" domain="[(('date').strftime('%%m'),'=', ((context_today()).strftime('%%m'))),(('date').strftime('%%d'),'>=', ((context_today()).strftime('%%d')))]"/>

我在域左侧有问题:('date'),系统说:AttributeError: object has no attribute 'strftime'。

尝试多种组合,但反应都是一样的。

你有想法吗?对象“日期”的类型是什么?

谢谢

【问题讨论】:

    标签: python date filter odoo


    【解决方案1】:

    这里是如何做到这一点的示例。在此示例中,我使用 write_date(即日期时间)=昨天搜索用户。

    yesterday = datetime.datetime.now() - datetime.timedelta(days = 2)
    yesterday_beginning = datetime.datetime(yesterday.year, yesterday.month, yesterday.day,0,0,0,0)
    yb = yesterday_beginning.strftime("%Y-%m-%d %I:%M:%S")
    today = datetime.datetime.now()
    today_beginning = datetime.datetime(today.year, today.month, today.day,0,0,0,0)
    tb = today_beginning.strftime("%Y-%m-%d %I:%M:%S")
    self.env['res.users'].search([('write_date','>=',yb),('write_date','<',tb)])
    

    【讨论】:

      【解决方案2】:

      如果您要按照说明过滤数据,则需要使用另一个字段来存储订单月份和日期。

      'order_month' : fields.char('Month')
      'order_day' : fields.char('Day')
      'order_year' : fields.char('Year')
      

      默认添加所有这些字段。

      'order_month' : lambda *a: str(time.strftime('%m')),
      'order_day' : lambda *a: str(time.strftime('%d')),
      'order_year' : lambda *a: str(time.strftime('%Y')),
      

      现在您可以直接在过滤中添加这些字段。

      【讨论】:

      • 我认为你的理由是:左操作数不能是对象,只能是字符串。所以我尝试了你的提议,但我无法在模型 sale_report 中添加字段。字段在模型中的 UI 中可见,但在数据库中不可见 您有什么想法吗? class sale_report(osv.osv): _inherit = 'sale.report' date_month = fields.Char(string='Date Month') date_day = fields.Char(string='Date Day')我没有错误信息。
      • 是的,在运算符的左侧,必须有一个数据库字段名称,您不能对其应用任何功能。要将这些字段更新到数据库中,您必须更新该模块。
      【解决方案3】:

      我找到了解决方案,sale.report 不是一个真实的模型,它是一个视图。对于添加字段,您需要覆盖 select 方法。 所以

      class sale_report(osv.osv):
          _inherit = 'sale.report' 
      
          date_order_month = fields.Char(string='Date Month')
          date_order_day = fields.Char(string='Date Day')
      
      
          def _select(self):
              select_str = """
                   SELECT min(l.id) as id,
                          l.product_id as product_id,
                          t.uom_id as product_uom,
                          sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,
                          sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,
                          count(*) as nbr,
                          s.date_order as date,
                          date_part('month', s.date_order) as date_order_month,
                          date_part('day', s.date_order) as date_order_day,
                          s.date_confirm as date_confirm,
                          s.partner_id as partner_id,
                          s.user_id as user_id,
                          s.company_id as company_id,
                          extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
                          s.state,
                          t.categ_id as categ_id,
                          s.pricelist_id as pricelist_id,
                          s.project_id as analytic_account_id,
                          s.section_id as section_id
              """
              return select_str
      

      【讨论】:

        【解决方案4】:

        您可以在 xml 中执行此操作,而不是通过 Python 代码来实现:

        <filter string="Current Month" domain="[('payment_date','&gt;=',context_today().strftime('%%Y-%%m-01')),('payment_date','&lt;',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01'))]"/>
        
        <filter string="Next Month" domain="[('payment_date','&gt;=',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01')),('payment_date','&lt;',(context_today()+relativedelta(months=2)).strftime('%%Y-%%m-01'))]"/>
        

        【讨论】:

          【解决方案5】:

          Odoo 将日期存储为 unicode 字符串。您实际上需要使用 datetime.datetime 类。 一个例子:

          datetime.datetime.strptime(&lt;YOUR DATE&gt;, "%Y-%m-%d %H:%M:%S").strftime('%%m')

          【讨论】:

          • 这行不通。我认为过滤器中域的左操作数中禁止使用函数
          • 将表达式的结果存储在一个新的字段中,您可以使用它。
          猜你喜欢
          • 1970-01-01
          • 2018-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-27
          • 1970-01-01
          • 2012-12-14
          • 2021-12-28
          相关资源
          最近更新 更多