简体   繁体   English

如何在Odoo 10上创建自动化任务?

[英]How to create automate tasks on Odoo 10?

I'm trying to create a task that runs periodicaly. 我正在尝试创建一个定期运行的任务。

I made this file on a directory called 'Data' and added it to the manifest file : 我在名为“Data”的目录上创建了这个文件,并将其添加到清单文件中:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
        <record id="ir_cron_scheduler_action" model="ir.cron">
            <field name="name">scheduler</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="interval_number">1</field>
            <field name="interval_type">minutes</field>
            <field name="numbercall">-1</field>
             <field eval="False" name="doall"/>
            <field eval="'res.company'" name="model"/>
            <field eval="'refresh'" name="function"/>
         </record>
   </data>

and I have the following method on that model 我在该模型上有以下方法

def refresh(self):
    _logger.info(' This is a test')

But it seems is not working, doesn't print on the logger. 但似乎不起作用,不在记录器上打印。 Any Idea? 任何想法?

  1. Add in you file xml <field eval="True" name="active" /> 添加文件xml <field eval="True" name="active" />
  2. Add in your code python 添加你的代码python

@api.model def refresh(self): _logger.info(' This is a test')

As @Karaka Mohamed said, you should add @api.model to your method. 正如@Karaka Mohamed所说,你应该在你的方法中添加@api.model And then, I would add some attributes to the XML like args , to specify empty arguments, or nextcall , in which you have to indicate the date of the first execution of the cron task. 然后,我会向XML添加一些属性,如args ,指定空参数或nextcall ,您必须在其中指明第一次执行cron任务的日期。 It should be a future date, unless you set the attribute doall to True (this will run the non-executed cron tasks of the past). 它应该是一个未来的日期,除非你将属性doall设置为True (这将运行过去的未执行的cron任务)。

From that nextcall date on, your cron task will be executed every minute forever (as you specified in your definition). 从下一个nextcall日期开始,您的cron任务将永远每分钟执行一次(如您在定义中指定的那样)。

An example, with this line your task will be executed for the first time five minutes after being generated: 例如,使用此行,您的任务将在生成后五分钟内首次执行:

<field name="args" eval="'()'"/>
<field name="nextcall" eval="(DateTime.now() + timedelta(minutes=5)).strftime('%Y-%m-%d 00:00:00')" />

Warning: as you have set <data noupdate="1"> at the top of your XML document, changes made here are not going to be updated in the database. 警告:由于您在XML文档的顶部设置了<data noupdate="1"> ,因此不会在数据库中更新此处所做的更改。 So, you can set these changes directly in PostgreSQL, in ir_cron table, or you can modify the XML ID of the cron task, to create a new one the following time you upgrade your module. 因此,您可以直接在PostgreSQL中,在ir_cron表中设置这些更改,或者您可以修改cron任务的XML ID,以便在以下升级模块时创建新的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM