Skip to Content
Menu
This question has been flagged
4 Replies
6089 Views

Maybe this is a silly question, but I don't know the answer.

I need a module that is able to split the input data into two different tables. I've understood that I have to create two different classes, but I don't know I can merge them into the same view.

I need only one form to put my data into and two tables with different attributes.

Thank you!

Avatar
Discard

Please give proper example of what exactly you want to do.

Best Answer

You can do this more simple using inheritance by delegation, using _inherits syntax in the model class. That way you could use in the view the fields of the other model like if they were declared in the same model and they will be saved in their own model table. You can have something like this:

 # -*- encoding: utf-8 -*-
from openerp.osv import fields, osv
from openerp import models

class mymodule_test(osv.osv):
    _name = 'mymodule.test'
    _description = 'mymodule
    
    _inherits = {'mymodule.2module': 'mod_id'}

    columns = {
        'name': fields.char('Name', size=128, help="Insert your name", required=True),
        'surname': fields.char('Surname', size=128, required=True),
        'mod_id': fields.many2one('mymodule.2module', string="mymodule"),
    }

class mymodule_2db(osv.osv):
    _name = 'mymodule.2module'
    _description = 'mymodule'
               
    _columns = {
        'select': fields.boolean('Select?', default=True),
    }

In the view:

 <?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="view_mymodule_tree" model="ir.ui.view">
         <field name="name">mymodule.test.tree</field>
         <field name="model">mymodule.test</field>
         <field name="arch" type="xml">
        <tree string="mymodule">
         <field name="name"/>
         <field name="surname"/>
<field name="select"/>
             </tree>
            </field>
        </record>
    </data>
</openerp>

more clean this way

Avatar
Discard
Best Answer

Yes you can do it just call create of second table model in first table model create method

For example


def create(self, cr, uid, values, context=None):

    values1 = {"name":values.get('name'),"street":values.get('address')}

    partner = self.pool.get("res.partner").create(cr, uid, values1);

Avatar
Discard
Author Best Answer

This is my original view:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_mymodule_tree" model="ir.ui.view">
<field name="name">mymodule.test.tree</field>
<field name="model">mymodule.test</field>
<field name="arch" type="xml">
<tree string="mymodule">
<field name="name"/>
<field name="surname"/>
     </tree>
</field>
</record>
</data>
</openerp>

and this is my model:

# -*- encoding: utf-8 -*-
from openerp.osv import fields, osv
from openerp import models

lass mymodule_test(osv.osv):
_name = 'mymodule.test'
_description = 'mymodule

columns = {

'name': fields.char('Name', size=128, help="Insert your name", required=True),
'surname': fields.char('Surname', size=128, required=True),

So, I have only one table (mymodule_test) with the attribure name and surname.

Now I want to add this class:

class mymodule_2db(osv.osv):
_name = 'mymodule.2module'
_description = 'mymodule'

_columns = {

'select': fields.boolean('Select?', default=True),
}

and I have updated my view:

<record id="view_mymodule_add" model="ir.ui.view"> 
<field name="name">mymodule.2module</field>
<field name="model">mymodule.2module</field>
<field name="arch" type="xml">
<form string="Address">
<sheet>
    <group name="group_top">
<group name="group_left">
    <field name="select" />
</group>
                   </group>
                   </sheet>
       </form>
   </field>
</record>

So I'm able to create anothe table (mymodule_2module) with the attribute 'select'.

I would like to have only one form (in the same page) where user can input 'name', 'surname' and could select 'select': 'name' and 'surname' must be writter in the table 'mymodule_test'; 'select' in the table 'mymodule_2module', using a foreign key.

Avatar
Discard