Help

2

What is the difference between normal context and _context ?

Hello Everyone,

In odoo , new coding standard we can have two context if we use api.

1) cr,uid, context = self.env.args,

2) self._context

We can get context two way, but value of both convext is different.

What is the different between these two context?

Thanks!

Regards,

Anil.    


2 Comments
Avatar
Discard
Avatar
Temur
-

Can you post please what kind of differences you've spotted? Examples?

_context is frozon dictionary cannot update, like we were able to update till v7, some of the information are different for both context in same method.

3 Answers
2
Best Answer

Hi Everyone,

When we extract both context which is implicity passed in any method having same information.

Like:

cr, uid, context = self.env.args
or

self._context

Both having same information even if you have passed any external key from client side, you will get the same in both context.

But first different I have suspected is,

while I was using context with recursion:

I was trying to passed the external key to context which is frozon dictionary or self._context to access info inside the same method.

by using with_context we can directly pass the our key to frozone dictionory or self._context

So, it was not showing the key in the next call of same method (Recursion).

Here is the syntax to pass any key using with_context

eg:

@api.multi
def custom_method(self)
#code here..
print "_context",self._context #so here i was not able to get the external key in next call.
if condition:
return True
self.with_context(key=value).custom_method() #recusrion

I found solution for it which works for me. Here is the sample example.

from openerp.tools import misc

@api.multi
def custom_method(self):
cr, uid, context = self.env.args

print "Context ::::",context
if condition:
return True
 
context = dict(context)
context.update({key:value})
self.env.args = cr, uid, misc.frozendict(context)
self.custom_method()

This is how I passed external key and got it, in the very next call of that function which was called within function.

Correct me if I am wrong!

If i will come up with more differences will update here.

Regards,

Anil.


Avatar
Discard