Odoo Help

3

how many2many defined for custom module [Closed]

By
Atchuthan
on 6/10/13, 5:31 AM 14,096 views

The Question has been closed

by
Atchuthan
on 12/13/2013 00:54:22
Hi guyz,
       I tried to use many2many relational field in my custom module "notebook". Code is given below:

notebook.py:

from osv import fields, osv
import time

class notebook(osv.osv):
    _name = "notebook"
    _description = "Simple Notebook"
    _columns = {
        'title' : fields.char('Title', size=30, required=True),
        'tag_ids': fields.many2many(
                    'hello',
                    'title',
                    'name',
                    string="Tags"
                                ),
    }

notebook()

class hello(osv.osv):
    _name = 'hello'
    _columns = {
            'name':fields.char('Name',size=30),
            'note_ids': fields.many2many(
                                'notebook',
                                'name',
                                'title',
                                string="Notebooks"
                                        ),
                    } 
hello()

notebook_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="notebook_form_view">
            <field name="name">notebook.form</field>
            <field name="model">notebook</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Notebook">
                    <field name="title" />
                    <field name="tag_ids" widget="many2many_tags"/>
                </form>
            </field>
        </record>

        <record model="ir.actions.act_window" id="action_notebook_form">
            <field name="name">notebook</field>
            <field name="res_model">notebook</field>
        </record>

        <menuitem name="NotebookParent" icon="terp-project" id="NotebookParent_menu" />

        <menuitem name="NotesChild" parent="NotebookParent_menu" id="NotesChild_menu" />

        <menuitem name="Header" parent="NotesChild_menu" id="Header_menu_mainform"
            action="action_notebook_form" />
    </data>
</openerp>

hello_view.xml:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="hello_form_view">
            <field name="name">hello.form</field>
            <field name="model">hello</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Hello Form">
                    <field name="name" select="1" />
                    <field name="note_ids" widget="many2many_list"/>
                </form>
            </field>
        </record>
    </data>
</openerp>

There is no error in installing and the fields are also displayed and data is stored in the Database.

My problem arises as inter-relationship between note_ids and tag_ids is not proper.

  Example:
     If I have a notebook record as :
           title = sample
           tags = tag1, tag2
     The tags are created in hello record as:
           tag1 record:
               name="tag1"
               Notebooks = ""
           tag2 record:
               name="tag2"
               Notebooks= ""

Why is the relationship not maintained?

2
Atchuthan
On 6/11/13, 7:07 AM
Hi friendz,
     after analysing through various sites, I inferred how to define a many2many relational field like below for the same example:

class notebook(osv.osv):
_name = "notebook"
_columns = {
    'title' : fields.char('Title', size=30, required=True),
    'tag_ids': fields.many2many(
                'hello',
                'notebook_hello_rel',
                'notebook_id',
                'hello_id',
                string="Tags"
                            ),
}
 notebook()

class hello(osv.osv):
_name = 'hello'
_columns = {
        'name':fields.char('Name',size=30),
        'note_ids': fields.many2many(
                            'notebook',
                            'notebook_hello_rel',
                            'hello_id',
                            'notebook_id',
                            string="Notebooks"
                                    ),
                } 
 hello()

In this a relationship table "notebook_hello_rel" is created automatically at the time of module install and the columns in it are "notebook_id", "hello_id".

3

ClueLogics Technologies Pvt. Ltd.

--ClueLogics Technologies Pvt. Ltd.--
6133
| 6 5 8
Delhi NCR, India
--ClueLogics Technologies Pvt. Ltd.--

 ClueLogics Technologies Pvt Ltd is a Software product and services provider. We are positioned to deliver robust, rapid and reliable Information Technology solutions that work. What we succeed upon is Technology and Technical Expertise. The industry review of ClueLogics has been commendable for the past years as most of its business comes from repeat orders by highly satisfied clients.

ClueLogics Technologies Pvt. Ltd.
On 6/10/13, 9:54 AM

Hi

class notebook(osv.osv):
_name = "notebook"
_description = "Simple Notebook"
_columns = {
    'title' : fields.char('Title', size=30, required=True),

}

 notebook()

This should like this, no need to add many2many here

class hello(osv.osv):
_name = 'hello'
_columns = {
        'name':fields.char('Name',size=30),
        'note_ids': fields.many2many(
                            'notebook',
                            'relation_table_note_hello',
                            'note_id',
                            'hello_id',
                             string="Notebooks"
                                    ),
                } 
  hello()

hope this will help you :)

Thanks
Sandeep

if there is no m2m field in notebook, how can it reference the relationship between the 2 tables in DB

Atchuthan
on 6/10/13, 11:04 AM

no need add it in notebook the relation is made on the given first para in many2many notbook thats it

ClueLogics Technologies Pvt. Ltd.
on 6/10/13, 11:09 AM

how can i access the tags from notebook_view.xml without having a m2m field there?

Atchuthan
on 6/11/13, 2:53 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)

Stats

Asked: 6/10/13, 5:31 AM
Seen: 14096 times
Last updated: 2/13/17, 11:00 PM