Odoo,Domain,过滤日期和月份

时间:2015-06-03 09:31:27

标签: python date filter odoo

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

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

我在域的左侧遇到问题:(' date'),系统说:AttributeError:object没有属性' strftime'。

尝试多种组合,但反应相同。

你有个主意吗?什么是对象的类型' date' ?

由于

5 个答案:

答案 0 :(得分:1)

如果您想在解释时过滤数据,则需要使用其他字段来存储月份和日期的订单。

'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')),

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

答案 1 :(得分:1)

我找到了解决方案,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

答案 2 :(得分:1)

以下是如何做到这一点的示例。在此示例中,我使用write_date(即datetime)=昨天搜索用户。

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)])

答案 3 :(得分:0)

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

datetime.datetime.strptime(<YOUR DATE>, "%Y-%m-%d %H:%M:%S").strftime('%%m')

答案 4 :(得分:0)

您可以通过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'))]"/>