2

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

笔记本.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>

你好_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= ""

为什么不维持关系?

4

1 回答 1

7

请看你的many2many 应该是这样的

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

例如:在你的情况下,两个 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"
                                    ),
于 2013-06-10T09:59:44.840 回答