我想自动执行操作,这是我正在使用的代码:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record id="ir_cron_scheduler_demo_action" model="ir.cron">
<field name="name">Demo scheduler</field>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">2</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="'note.admin'" name="model"/>
<field eval="'process_demo_scheduler_queue'" name="function"/>
</record>
</data>
</openerp>
@api.model
def process_demo_scheduler_queue(self):
for note in self.env['note.admin']:
if datetime.strptime(note.date, DEFAULT_SERVER_DATETIME_FORMAT).date() == datetime.now().date():
note.write({'is_visible': True})
我想做的是在字段note.date ==当前日期
时将值is_visible设置为True这是登录服务器:
2016-05-25 01:20:17,658 3680 INFO test3 werkzeug:127.0.0.1 - - [25 / May / 2016 01:20:17]“POST /web/dataset/call_kw/note.admin/message_get_subscription_data HTTP / 1.1“200 -
但它不起作用!
解决:
@api.model
def process_demo_scheduler_queue(self):
notes = self.search([('date', '=', fields.Date.today())])
notes.write({'is_visible': True})
答案 0 :(得分:2)
你必须写下面的代码,
@api.model
def process_demo_scheduler_queue(self):
for note in self.env['note.admin'].search([]):
if datetime.strptime(note.date, DEFAULT_SERVER_DATETIME_FORMAT).date() == datetime.now().date():
note.write({'is_visible': True})
答案 1 :(得分:1)
首先,您必须搜索数据库中的所有注释,然后再尝试使用odoo的日期逻辑:
@api.model
def process_demo_scheduler_queue(self):
for note in self.search([]):
if note.date == openerp.fields.Date.context_today(self):
note.is_visible = True
或者更快:
@api.model
def process_demo_scheduler_queue(self):
notes = self.search([('date', '=', openerp.fields.Date.context_today(self))])
notes.write({'is_visible': True})