How to make a "parent-child" module

Asked by Bino

Dear All ..

Is there any example of parent-child module that "simple but complete" (including the "view") ?
I try to understand on how to do it by read the forum and dev-book, but all came with partial info and most of them interact with the "res" ... it's to big for my brain.
Maybe a simple sample with :
1. Parent : a class with 1 or 2 field
2. Child : a class with 2 or 3 field, 1 of it a link to the parent

Sincerely
-bino-

Question information

Language:
English Edit question
Status:
Solved
For:
OpenERP Edit question
Assignee:
No assignee Edit question
Solved by:
Nicolas DS
Solved:
Last query:
Last reply:
Revision history for this message
Best Nicolas DS (zyphos) said :
#1

my_module/__init__.py
import my_module # import my_module.py

my_module/__terp__.py
{
    "name" : "This is my module",
    "version" : "1.0",
    "author" : "It's me",
    "category" : "Generic Modules",
    "depends" : [], # base module is always loaded, so you don't need to put it.
    "init_xml" : [],
    "demo_xml" : [],
    "update_xml" : ["my_module_view.xml"],
    "installable": True,
    "active": True
}

my_module/my_module.py
class parent (osv.osv):
    _name = 'my_module.parent'
    _columns = {
        'name': fields.char('Parent Name', size=64, required=True, translate=True),
        'sequence': fields.integer('Sequence')
    }
Parent() # Info: Instanciate class

class child(osv.osv):
    _name = 'my_module.child'
    _columns = {
        'name': fields.char('Child Name', size=64, required=True, translate=True),
        'hits': fields.integer('Number of hit'),
        'parent': fields.many2one('my_module.parent','My parent', required=True),
    }

You don't need to create a view, indeed a default view is automatically created for object without one.
But you need to create a menu to access to your module.

my_module_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <!-- create main menu -->

        <menuitem id="menu_main_my_module" name="My Module"/>

        <!-- Menu that open new tab need action -->

        <record id="my_module_parent_form" model="ir.actions.act_window">
            <field name="name">Parents</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">my_module.parent</field>
            <field name="view_type">form</field>
        </record>

        <!-- Menu linked to an action, to browse parent -->

        <menuitem action="my_module_parent_form" id="menu_my_module_parent_action_form" parent="my_module.menu_main_my_module" sequence="1"/>

        <!-- Do the same for child -->

        <record id="my_module_child_form" model="ir.actions.act_window">
            <field name="name">Childs</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">my_module.child</field>
            <field name="view_type">form</field>
        </record>
        <menuitem action="my_module_child_form" id="menu_my_module_child_action_form" parent="my_module.menu_main_my_module" sequence="1"/>
    </data>
</openerp>

Revision history for this message
Bino (bino) said :
#2

Dear Sir

Thankyou for your enlightment.

I made a copy of your script.

Test :
1. I create the first parent record with parentname = 123 , save
2. I create the first child record, When I put "345" as parentname .. it pop up a lookup window, but there is no any record from the parent.
So, from that lookup window .. I create a new parent record with parentname=345 ... save ... and it automaticaly copied to parentname field of child. Save
3. I try to create second child record. When I put "789" as the parent name, it pop up a lookup window, buth this time it list completely previouse created parent record wit parentname as "123" and "345" respectively.

I don't know whats the problem with step#2

BTW ... what I want is that :
1. There is no "Direct access" to CRUD the child
2. First have to access the parent
3. And from the parent table , user can create the child ... just like the view of "New Bill of Materials" of MRP module.
4. When creating the child, parentname field of child is not editable it's feeded with "name" field of parent
5. just like the view of "New Bill of Materials" of MRP module.

Again, Thankyou for your enlightment ..

Sincerely
-bino-

Revision history for this message
Nicolas DS (zyphos) said :
#3

Sorry for the code I forgot at the beginning "from osv import osv, fields", at the end "child()" and "parent()" instead of "Parent()" in my_module.py

For step #2:
It's normal, because if you enter something in the parent field before browsing all parents, it will search parent that have the content of this field in their name ! If you press search again or if you let the field empty you will get every parent records.

1. Simply remove the access menu, code below <!-- Do the same for child -->
3. You need a one2many field in parent: 'child_ids': fields.one2many('my_module.child', 'parent', 'Childs'),
Open Object auto generate view don't support one2many, so we have to create the view.
Add this in "my_module_view.xml" below last 'menuitem'

        <record id="view_parent_form" model="ir.ui.view">
            <field name="name">my_module.parent.form</field>
            <!-- You can set name that you want, it's only important if you want to refer to it, example, force a special view -->
            <field name="model">my_module.parent</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Parents">
                    <field name="name"/>
                    <field name="sequence"/>
                    <field name="child_ids" colspan="4" nolabel="1" select="1" >
                        <!-- Each form is made of 4 columns, you can make a "group" to change this value -->
                        <form string="Childs">
                            <field name="name"/>
                            <field name="hits"/>
                        </form>
                        <tree string="Childs">
                            <field name="name"/>
                            <field name="hits"/>
                        </tree>
                    </field>
                </form>
            </field>
        </record>

Don't forget to restart your server. Python code modification aren't affective with a simple module reload for now.

It's all.

Revision history for this message
Bino (bino) said :
#4

Dear Sir.

I really appreciate your help.
It work ...

Your are the end of my 2 week brain torture

Sincerely
-bino-

Revision history for this message
Bino (bino) said :
#5

Thanks Nicolas DS, that solved my question.