تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
6737 أدوات العرض
I'm trying to create a cron job that saves subcriptions on Odoo database.
The "save_subscription" method works fine when called from a button, but when it is
called from the cron job, I get an empty recordset. I tried logging "self" and
get the following:

When called from cron:
SELF res.company()

When called from button:
SELF res.company(1,)

When executing the cron job, every model I try to access with self.env['model'] is also empty.
I've tried changing the decorator to @api.multi but no luck. I've also tried this answer https://www.odoo.com/pt_BR/forum/help-1/question/how-to-call-a-scheduled-task-cron-using-the-new-api-v8-90924 but I still get an empty recordset.

Can anyone tell me how to access my records through a cron job? I feel like I'm doing a silly mistake but I've tried everything and couldn't find it.

Thanks in advance!


@api.model
def cron_get_new_subscriptions(self):
    ...
    ...
    if subscriptions['items']:
       for sub in subscriptions['items']:
            splitDate = sub['createdAt'].split('T')[0].split('-') created_at = datetime(int(float(splitDate[0])),             int(float(splitDate[1])), int(float(splitDate[2]))).date()
            self.save_subscription(sub, created_at == today.date())
       return

@api.multi     
def save_subscription(self, item, isNew):
    _logger.info("\n\nSELF %s", self)
    ...
    ...

<record id="cron_get_new_subs" model="ir.cron">
    <field name="name">Cron Get New Subscriptions</field>
    <field name="user_id" ref="base.user_root" />
    <field name="interval_number">15</field>
    <field name="state">code</field>
    <field name="interval_type">minutes</field>
    <field name="numbercall">-1</field>
    <field eval="False" name="doall" />
    <field name="model_id" ref="model_res_company"/>
    <field name="code">model.cron_get_new_subscriptions()</field>
</record>
الصورة الرمزية
إهمال
أفضل إجابة

Hello Arthur,

Cron job function self will give always empty browse object only

So you have search the company or you can get it from self.env.user.company_id

الصورة الرمزية
إهمال
الكاتب

I see. And how would I access my self.env['sale.order'] from inside a cron, for example? How do I search for it?

الكاتب

Nevermind, I got it. Basically I was trying to get access to a my model variables through "self.variableName", but since self was empty I got errors.

Thanks, subbarao!

الكاتب

Nevermind, I got it. Basically I was trying to get access to a my model variables through "self.variableName", but since self was empty I got errors.

Thanks, subbarao!

أفضل إجابة

Settings -> Activate the developer mode

Settings - Technical -> Automation -> Scheduled Actions

Select your cronjob -> Run Manually

You can use pycharm community to debug

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
3
سبتمبر 23
12043
0
أكتوبر 24
2466
1
أغسطس 24
5379
0
مايو 21
3843
2
يناير 25
3107