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.

0

How to pass data from a class to another

By
Uillino
on 8/18/15, 4:57 AM 1,096 views

I have two classes:

class first_class(models.Model):
_name = 'tbl1'
code = fields.Char('Code', size=3, required=True)
description = fields.Char('Description', size=40, required=True)

class second_class(models.Model):
_name = 'tbl2'
_inherits = {'tbl1': 'id_tbl1'}

id_tbl1 = fields.Many2one('tbl1')
employee = fields.Char('Name', size=40, required=True)

When I create a new row with the first class, I would like to go directoly to the second class with the id I've just created. That is I would like to pass the id of tbl1 to tbl2.

Is it possible?

This is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<record id="view_mymodule_tbl1_tree" model="ir.ui.view">
<field name="name">mymodule.tbl1.tree.view</field>
<field name="model">tbl1</field>
<field name="arch" type="xml">
<tree string="Bridge" export="false">
<field name="id"/>
<field name="code"/>
<field name="description"/>
</tree>
</field>
</record>


<record id="view_mymodule_tbl1_form" model="ir.ui.view">
<field name="name">mymodule.tbl1.form.view</field>
<field name="model">tbl1</field>
<field name="arch" type="xml">
<form string="Bridge" duplicate="false">
<header>
<button name="%(action_mymodule_tbl2)d" type="action" string="Submit" />
</header>
<sheet>
<group>
<field name="code"/>
<field name="description"/>

</group>
</sheet>
</form>
</field>
</record>

<record id="action_mymodule_tbl1" model="ir.actions.act_window">
<field name="name">action tbl1</field>
<field name="res_model">tbl1</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="search_view_id" eval="False"/>
</record>

<menuitem action="action_mymodule_tbl1" id="menu_action_mymodule_tbl1" parent="mail.mail_feeds" sequence="140"/>

<record id="view_mymodule_tbl2_tree" model="ir.ui.view">
<field name="name">mymodule.tbl2.tree.view</field>
<field name="model">tbl2</field>
<field name="arch" type="xml">
<tree string="Mymodule" export="false">
<field name="id_tbl1" readonly="1"/>
<field name="employee
</tree>
</field>
</record>

<record id="view_mymodule_tbl2_form" model="ir.ui.view">
<field name="name">mymodule.tbl2.form.view</field>
<field name="model">tbl2</field>
<field name="arch" type="xml">
<form string="Mymodule" duplicate="false">
<group>
<field name="id_tbl1" readonly="1"/>
<field name="employee"/>
</group>
</form>
</field>
</record>
 
<record id="action_mymodule_tbl2" model="ir.actions.act_window">
<field name="name">Insert</field>
<field name="res_model">tbl2</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="view_mymodule_tbl2_form" />
<field name="target">new</field>
</record>

<menuitem action="action_mymodule_tbl2" id="menu_action_mymodule_tbl2" parent="mail.mail_feeds" sequence="150"/>


</data>
</openerp>

1
dsfairy84
On 8/19/15, 7:48 AM

EDIT:

in your code replace:

<button name="%(action_mymodule_tbl2)d" type="action" string="Submit" /> 

with

<button name="open_second_class" type="object" string="Submit" /> 


And in your class tbl1 add this method:

@api.multi
def open_second_class(self):
ac = self.env['ir.model.data'].xmlid_to_res_id('YOURMODULE.view_mymodule_tbl2_form', raise_if_not_found=True)
tbl1 = False
for o in self:
tbl1 = o.id
result = {
'name': '2nd class',
'view_type': 'form',
'res_model': 'tbl2',
'view_id': ac,
'context': {'default_id_tbl1': tbl1},
'type': 'ir.actions.act_window',
'view_mode': 'form'
}
return result

regards,
Disha


Thank you for your reply, Disha. Could you make an example based on my code, please?

Uillino
on 8/19/15, 9:00 AM

I edited the answer for code example. please check.

dsfairy84
on 8/20/15, 4:34 AM

I don't know how to thank you: it works!

Uillino
on 8/20/15, 8:50 AM
0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 8/18/15, 5:09 AM

The idea is to  save it to the context dictionary, from the other class use context.get('id_of_table2',False)

Thank you for your reply. Could you make an example, please? I don't now how context works.

Uillino
on 8/18/15, 6:15 AM
0

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 8/18/15, 5:17 AM

Friend!!

Try to follow this example.

https://openerpdev.wordpress.com/2012/01/31/openerp-one2many-many2one-fields-example/

Regards

Thnak you for your help, but I don't think that this is my case.

Uillino
on 8/18/15, 9:35 AM
0
Uillino
On 8/19/15, 5:25 AM

Sorry, but I'm not able to solve this problem yet.

Any help, please?

0

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 8/18/15, 7:02 AM

In second class create method and call (Refer Product Module product.py File)

Example:-

    def _get_tbl1_ids(self):
tbl1_ids = self.env['tbl1'].search([('id_tbl1', 'in', self.ids)])
return list(set(tbl1_ids))

I'm sorry, but I'm not able to call this as a related field in Odoo 8. Could you hel me, please?

Uillino
on 8/19/15, 5:14 AM

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

1 follower(s)

Stats

Asked: 8/18/15, 4:57 AM
Seen: 1096 times
Last updated: 8/20/15, 8:50 AM