为自定义模块定义了多少2个

时间:2013-06-10 09:52:08

标签: openerp

我尝试在自定义模块“notebook”中使用many2many关系字段。代码如下:

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>

安装时没有错误,并且还显示字段,数据存储在数据库中。

我的问题出现了,因为note_ids和tag_ids之间的相互关系不合适。

示例:

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= ""

为什么不保持这种关系?

1 个答案:

答案 0 :(得分:7)

请看看你的很多人 它应该是这样的

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

例如:在你的情况下,两个many2many可能是这样的

first many2many

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

第二次多2

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