问题
I have many crons that call the same method at different time and the method needs to find that which cron called it to find other record based on that cron.
The args mustn't be fixed. There'll be the number of crons create by end user on the front end side of odoo so whenever the cron executes and call the method the cron should pass it's own id to the method so it should be dynamic.
Python Method that call by cron:
@api.model
def send_feedback_email_cron(self,id):
#id is needed to find global channel in which this cron is set.
global_channel = self.global_channel_id.search([('cron_id','=',id)])
for rule in global_channel.email_rule_ids:
rule.send_customer_review_email()
return True
Cron code:
<record id="ir_cron_send_customer_feedback_email_job" model="ir.cron">
<field name="name">Send Feedback Email to customer</field>
<field eval="False" name="active"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">4</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="ref('customer_review_global_channel_ept.model_email_global_channel_rule_ept')" name="model_id" />
<field name="state">code</field>
<field name="code">model.send_feedback_email_cron()</field>
</record>
回答1:
Use ir.cron
function
and args
fields:
<record id="ir_cron_send_customer_feedback_email_job" model="ir.cron">
<field name="name">Send Feedback Email to customer</field>
<field eval="False" name="active"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">4</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="ref('customer_review_global_channel_ept.model_email_global_channel_rule_ept')" name="model_id" />
<field name="function" eval="'send_feedback_email_cron'"/>
<field name="args" eval="'(...your args...)'"/>
</record>
来源:https://stackoverflow.com/questions/50961436/how-to-pass-cron-id-as-arg-in-method-that-the-cron-calls