Showing posts with label context. Show all posts
Showing posts with label context. Show all posts
Saturday, October 6, 2012
_defaults & context when click create object
This topic is very interesting. I'm studying to resolve this problem and I found this solution by my self.
problem :
I have predefined ORM create method in my module for model product.product. Also, I have custom view for that model. My custom view appears only in my module section, but for everything else appears default product.product view.
My problem is when user click on create button, how can I know is that click was generated from my custom view. I need this because some additional processing before creating product.product record.
I find solution via context. So, I put context in some field in my custom form view:
<openerp>
<data>
<record id="custom_form_view" model="ir.ui.view">
....
<field name="is_custom" context="{'identificator':True}" />
.....
</record>
</data>
</openerp>
In my predefined create method I will check this with if/else statement:
if context.has_key('identifikator'):
... aditional code ...
But, this solution doesn't work. I can't see context="{'identificator':True}" in create method, only default context appears.
How can I pass custom context from form view to create method on this or some other way?
Solution is : Add context="{'identificator':True}" in action record like below
<record id="act_obj1" model="ir.actions.act_window">
<field name="name">Obj1</field>
<field name="res_model">test.mod.obj1</field>
<field name="context">context="{'identificator':True}</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
source : http://www.openerp.com/forum/topic34382.html
Saturday, September 29, 2012
pass context from front-end(view) to back-end(server)
>>> model :
....
class obj1(osv.osv):
_name = "test.modex.obj1"
_columns = {
'name': fields.char('name', size=50, select=True, required=True),
'surname': fields.char('surname', size=50, select=True),
'grade': fields.char('Grade', size=2, select=True),
}
def onchange_test(self, cr, uid, ids, name, surname, context):
print '-- onchange_test --'
print context
return {}
def my_func(self, cr, uid, ids, context={}):
# do stuff using the info passed in the context variable...
print '-- my_func --'
print context
.....
>>> view :
....
<record id="view_obj1_form" model="ir.ui.view">
<field name="name">test.modex.obj1.form</field>
<field name="model">test.modex.obj1</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form>
<field name="name"/>
<field name="surname" on_change="onchange_test(name,surname,context)" context="{'myname' : name, 'mygrade':grade}"/>
<field name="grade"/>
<newline/>
<button string="Click Me" type="object" name="my_func" context="{'param' : 'value'}" />
</form>
</field>
</record>
.....
>>> view on browser :

>>> result on server :
-- onchange_test --
{'lang': 'en_US', 'tz': False, 'uid': 3, 'mygrade': 'A', 'myname': 'xxxx'}
-- my_func --
{'lang': 'en_US', 'tz': False, 'uid': 3, 'param': 'value'}
context and domain
Domain is used purely for filtering (e.g. on Menu Actions, to control the records that are available in the tree/list and form views)
Context, as its name suggests, is used to pass arbitrary contextual information to the server.
For example, on a one2many field on the partner object, you may put context="{'partner_id' : active_id}". This information is then available to the server when generating the forms that pop up from the one2many field, for example in '_defaults' field functions. However the object is not (necessarily) filtered by anything you put into the context, only if the object designer has decided that it should be.
One neat little trick actually is to use the context to pass in default values for fields. E.g. to pass in a default value for a 'name' field, use: context="('default_name' : 'Fred')"
Another use for Context is on buttons. You can use it to pass parameters to object functions. E.g.:
View XML:
Code:
<button string="Click Me" type="object" name="my_func" context="{'param' : 'param_val'}" />
Object Code:
Code:
def my_func(self, cr, uid, ids, context={}):
# do stuff using the info passed in the context variable...
# do stuff using the info passed in the context variable...
Subscribe to:
Posts (Atom)