vettloffah / odoo-await

Odoo API client featuring async await.
74 stars 29 forks source link

update with context #28

Closed DonsWayo closed 2 years ago

DonsWayo commented 2 years ago

Hi,

Is posible to update a model with context?

like this:

odoo.update('product.template', 222, {short_description: '3M Hazard Warning Tape ', context: { lang: 'en_GB' }});

I get a true response, but not updated on odoo. Can do in some way?

thanks

vettloffah commented 2 years ago

That's not currently built into the library.

You could try using the execute_kw(model, method, params) method as a work around.

something like:

await odoo.execute_kw('product.template', 'update', [ [222 , {short_description: "3M Hazard Warning Tape"}, { context: { lang: 'en_GB'  } } ] ]

Could you let me know if that works?

DonsWayo commented 2 years ago

Thanks for the suggestion but not working.

I get this error:

TypeError: cannot marshal None unless allow_none is enabled.

I see other examples like this:

api.execute_kw(db, uid, password, 'res.partner', 'write', [[id], {'name': "Atul Arvind"}], {'context' :{'tag': 1}})

When try to do the same.

await odoo.execute_kw('product.template', 'write', [[id], { name: '3M Hazard Warning Tape' }], { context: { lang: 'en_GB' } });

get this response: result = method(recs, *args, **kwargs) TypeError: write() got an unexpected keyword argument 'name' or TypeError: write() got an unexpected keyword argument 'short_description'

Same response with update or write

vettloffah commented 2 years ago

@DonsWayo the "marshal none" error is actually just odoo not returning anything, which is not actually usually an error. It's because the XML RPC settings in odoo by default are set to not allow it to return nothing. You can catch this error and ignore it.

vettloffah commented 2 years ago

Here is an example I did recently:

 let paymentId = 0;
 try{
        paymentId = await odoo.create("account.payment", payment);
        await odoo.execute_kw("account.payment", "action_post", [[paymentId]]);
        return paymentId;
    }catch(e){
        if(e instanceof Error){
            // Odoo returns an allow_none error, even if it successfully posts.
            // @ts-ignore
            if(e.faultString?.includes("cannot marshal None unless allow_none is enabled")){
                return paymentId;
            }
        }
        // otherwise it's an unknown error
        throw e;
    }