Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

What is the difference between normal context and _context ?

By
Anil R. Kesariya
on 6/30/15, 3:10 AM 809 views

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.    


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

Temur
on 6/30/15, 3:46 AM

_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.

Anil R. Kesariya
on 6/30/15, 6:30 AM
2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 7/7/15, 12:50 PM

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.


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 6/30/15, 3:10 AM
Seen: 809 times
Last updated: 7/10/15, 4:45 AM