how many2many defined for custom module

为君一笑 提交于 2019-12-13 16:16:57

问题


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?


回答1:


Please take a look at your many2many It should be like this

fields.many2many('that object name',
             'sql relation table name',
             'this object id',
             'that object id',
             'Field Lable')

Eg: in your case the two many2many could be like this

first many2many

'tag_ids': fields.many2many(
                'hello',
                'notebook_hello_rel',
                'notebook_id',
                'hello_id',
                string="Tags"
                            ),

Second many2many

'note_ids': fields.many2many(
                            'notebook',
                            'notebook_hello_rel',
                            'hello_id',
                            'notebook_id',
                            string="Notebooks"
                                    ),


来源:https://stackoverflow.com/questions/17021105/how-many2many-defined-for-custom-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!