Odoo,域,过滤日期和月份
Odoo , Domain, filter date and month
我尝试在 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:对象没有属性 'strftime'。
尝试多种组合,但反应都是一样的。
你有想法吗?对象的类型是什么 'date' ?
谢谢
Odoo 将日期存储为 unicode 字符串。你实际上需要使用 datetime.datetime class。
一个例子:
datetime.datetime.strptime(<YOUR DATE>, "%Y-%m-%d %H:%M:%S").strftime('%%m')
如果你想像你解释的那样过滤数据,你需要用另一个字段来存储订单的月份和日期。
'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不是真实模型,是视图。对于添加字段,您需要覆盖 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
这是您如何操作的示例。在此示例中,我搜索 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)])
无需通过 Python 代码实现,您可以在 xml 中执行此操作,例如:
<filter string="Current Month" domain="[('payment_date','>=',context_today().strftime('%%Y-%%m-01')),('payment_date','<',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01'))]"/>
<filter string="Next Month" domain="[('payment_date','>=',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01')),('payment_date','<',(context_today()+relativedelta(months=2)).strftime('%%Y-%%m-01'))]"/>
我尝试在 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:对象没有属性 'strftime'。
尝试多种组合,但反应都是一样的。
你有想法吗?对象的类型是什么 'date' ?
谢谢
Odoo 将日期存储为 unicode 字符串。你实际上需要使用 datetime.datetime class。 一个例子:
datetime.datetime.strptime(<YOUR DATE>, "%Y-%m-%d %H:%M:%S").strftime('%%m')
如果你想像你解释的那样过滤数据,你需要用另一个字段来存储订单的月份和日期。
'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不是真实模型,是视图。对于添加字段,您需要覆盖 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
这是您如何操作的示例。在此示例中,我搜索 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)])
无需通过 Python 代码实现,您可以在 xml 中执行此操作,例如:
<filter string="Current Month" domain="[('payment_date','>=',context_today().strftime('%%Y-%%m-01')),('payment_date','<',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01'))]"/>
<filter string="Next Month" domain="[('payment_date','>=',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01')),('payment_date','<',(context_today()+relativedelta(months=2)).strftime('%%Y-%%m-01'))]"/>