Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
6753 Lượt xem
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>
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả

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!

Tác giả

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!

Câu trả lời hay nhất

Settings -> Activate the developer mode

Settings - Technical -> Automation -> Scheduled Actions

Select your cronjob -> Run Manually

You can use pycharm community to debug

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 9 23
12056
0
thg 10 24
2468
1
thg 8 24
5409
0
thg 5 21
3856
2
thg 1 25
3117