为自定义模块定义了多少个 [英] how many2many defined for custom module
本文介绍了为自定义模块定义了多少个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在自定义模块笔记本"中使用many2many关系字段.代码如下:
I tried to use many2many relational field in my custom module "notebook". Code is given below:
notebook.py:
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:
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:
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.
我的问题是由于note_ids和tag_ids之间的相互关系不合适而引起的.
My problem arises as inter-relationship between note_ids and tag_ids is not proper.
示例:
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?
推荐答案
请看看您的many2many 应该是这样
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
头多2多
'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"
),
这篇关于为自定义模块定义了多少个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文