Search 1.9 billion lines of Odoo code on GitHub

demo_scheduler

Author: My Company
License: no license
Branch: 14.0
Repository: twtrubiks/odoo-demo-addons-tutorial
Dependencies: base
Languages: Markdown (74, 52.9%), Python (26, 18.6%), and XML (40, 28.6%)
Other branches: master

<h1>odoo 觀念 - scheduler</h1> <p>建議觀看影片, 會更清楚:smile:</p> <p><a href="https://youtu.be/uvQTHsKu3Ic">Youtube Tutorial - odoo 手把手教學 - scheduler</a></p> <p>建議在閱讀這篇文章之前, 請先確保了解看過以下的文章 (因為都有連貫的關係)</p> <p><a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_odoo_tutorial">odoo 手把手建立第一個 addons</a></p> <p>本篇文章主要介紹 odoo 中的 scheduler 這部份</p> <h2>說明</h2> <p>先來看 <a href="models/models.py">models/models.py</a></p> <p>```python ......</p> <p><em>logger = logging.getLogger(</em><em>name</em>_)</p> <p>class DemoScheduler(models.Model): _name = &#39;demo.scheduler&#39; _description = &#39;Demo Scheduler&#39;</p> <pre><code>def action_schedule(self): _logger.warning(&#39;============= Action Schedule ==================&#39;) </code></pre> <p>```</p> <p>簡單定義一個 class, 裡面就只有一個 function ( 用來測試 schedule ),</p> <p>雖然沒有定義 fields, 但是還是會在 db 中建立 model <code>demo_scheduler</code>,</p> <p><img src="https://i.imgur.com/w8ztB9s.png" alt="alt tag"></p> <p>也請記得設定 security</p> <p><a href="security/ir.model.access.csv">security/ir.model.access.csv</a></p> <p><a href="security/security.xml">security/security.xml</a></p> <p>接著看 <a href="views/scheduler.xml">views/scheduler.xml</a>, schedule 的重點</p> <p><code>xml &lt;?xml version=&quot;1.0&quot; ?&gt; &lt;odoo&gt; &lt;data noupdate=&quot;0&quot;&gt; &lt;record id=&quot;demo_scheduler&quot; model=&quot;ir.cron&quot;&gt; &lt;field name=&quot;interval_type&quot;&gt;days&lt;/field&gt; &lt;field name=&quot;name&quot;&gt;demo scheduler&lt;/field&gt; &lt;field name=&quot;numbercall&quot;&gt;-1&lt;/field&gt; &lt;field name=&quot;priority&quot;&gt;5&lt;/field&gt; &lt;field name=&quot;doall&quot;&gt;False&lt;/field&gt; &lt;field name=&quot;active&quot;&gt;True&lt;/field&gt; &lt;field name=&quot;interval_number&quot;&gt;1&lt;/field&gt; &lt;field name=&quot;model_id&quot; ref=&quot;model_demo_scheduler&quot;/&gt; &lt;field name=&quot;state&quot;&gt;code&lt;/field&gt; &lt;field name=&quot;code&quot;&gt;model.action_schedule()&lt;/field&gt; &lt;/record&gt; &lt;/data&gt; &lt;/odoo&gt; </code></p> <p><code>interval_type</code> 分, 小時, 天, 禮拜, 月, 最小單位為 1 分鐘:exclamation::exclamation:</p> <p><code>interval_number</code> 次數, 搭配 <code>interval_type</code>, 像範例就是一天執行一次.</p> <p><code>numbercall</code> <code>-1</code> 代表不限制(無線循環), 如果今天設定為 <code>2</code>, 代表執行兩次之後就不會執行了.</p> <p><code>model_id</code> 指定 model.</p> <p><code>state</code> 使用的方式, 這邊使用 python code.</p> <p><code>code</code> 呼叫 <code>model.action_schedule()</code> models 中的 methond.</p> <p><code>priority</code> 0 最優先, 10最不重要(不優先).</p> <p>安裝完 addons 之後, 也可以到 odoo 中的後台查看 schedule, 請到以下的路徑</p> <p>Technical -&gt; Automation -&gt; Scheduled Actions</p> <p><img src="https://i.imgur.com/JFZD2Io.png" alt="alt tag"></p> <p>你應該會看到一個 demo schedule</p> <p><img src="https://i.imgur.com/PVvYzl0.png" alt="alt tag"></p> <p>你也可以在這邊改細項(設定)</p> <p><img src="https://i.imgur.com/EOsdBGg.png" alt="alt tag"></p> <p>Run Manually</p> <p>也可以手動觸發 schedule, 確保他是正常的:smile:</p> <p><img src="https://i.imgur.com/OrCh1mr.png" alt="alt tag"></p> <p>最後記得也要將 <code>scheduler.xml</code> 加入 <code>__manifest__.py</code> 哦</p> <p>```python ...... # any module necessary for this one to work correctly &#39;depends&#39;: [&#39;base&#39;],</p> <pre><code># always loaded &#39;data&#39;: [ &#39;security/security.xml&#39;, &#39;security/ir.model.access.csv&#39;, &#39;views/scheduler.xml&#39;, ], &#39;application&#39;: True, </code></pre> <p>} ```</p>