OdooOdooodoo

Odoo10开发教程三(模型关联)

2017-02-28  本文已影响3808人  luohuayong

模型关联

一个模型中的记录可能关联到另一个模型中的记录。例如,销售订单记录会关联到一个包含客户数据的客户记录中;同时销售订单记录也会关联到销售订单明细记录。

练习建立一个授课模型
在开放学院模块中,我们考虑一个授课模型:一个授课是在给定的时间中对给定的受众教授指定的课程。为授课建立模型,授课包括名称、开始时间、持续时间和席位数。添加操作和菜单项来显示新的模型。

openacademy/models.py

    name = fields.Char(string="Title", required=True)
    description = fields.Text()


class Session(models.Model):
    _name = 'openacademy.session'

    name = fields.Char(required=True)
    start_date = fields.Date()
    duration = fields.Float(digits=(6, 2), help="Duration in days")
    seats = fields.Integer(string="Number of seats")

openacademy/views/openacademy.xml

        <!-- Full id location:
             action="openacademy.course_list_action"
             It is not required when it is the same module -->

        <!-- session form view -->
        <record model="ir.ui.view" id="session_form_view">
            <field name="name">session.form</field>
            <field name="model">openacademy.session</field>
            <field name="arch" type="xml">
                <form string="Session Form">
                    <sheet>
                        <group>
                            <field name="name"/>
                            <field name="start_date"/>
                            <field name="duration"/>
                            <field name="seats"/>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <record model="ir.actions.act_window" id="session_list_action">
            <field name="name">Sessions</field>
            <field name="res_model">openacademy.session</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>

        <menuitem id="session_menu" name="Sessions"
                  parent="openacademy_menu"
                  action="session_list_action"/>
    </data>
</odoo>

注意
digits=(6,2)指定浮点数的精度:6是数字的总和,而2是小数位长度,这同时表明整数位的最大长度是4

关联字段

关联字段链接同一模型(不同层次结构)或者不同模型之间的记录。关联字段有三种类型:
Many2one(other_model, ondelete='set null')
一个链接到其它对象的简单示例是这样的:

print foo.other_id.name

One2many(other_model, related_field)
这是一个虚拟的关联,是Many2one的逆,One2many作为记录的容器,访问它将返回一个记录集(也可能是一个空记录集):

for other in foo.other_ids:
    print other.name

警告

因为One2many是一个虚拟关联,所以必须有一个Many2one字段存在于other_model,其名称也必须是related_field

Many2many(other_model)
双向多对多关联,一方的任一记录可以与另一方的任意数量记录关联。作为记录的容器,访问它也可能导致返回空记录集

for other in foo.other_ids:
    print other.name

练习Many2one关联
编辑CourseSession模型以反映他们与其它模型的关联:

openacademy/models.py

    name = fields.Char(string="Title", required=True)
    description = fields.Text()

    responsible_id = fields.Many2one('res.users',
        ondelete='set null', string="Responsible", index=True)


class Session(models.Model):
    _name = 'openacademy.session'
    start_date = fields.Date()
    duration = fields.Float(digits=(6, 2), help="Duration in days")
    seats = fields.Integer(string="Number of seats")

    instructor_id = fields.Many2one('res.partner', string="Instructor")
    course_id = fields.Many2one('openacademy.course',
        ondelete='cascade', string="Course", required=True)

openacademy/views/openacademy.xml

                    <sheet>
                        <group>
                            <field name="name"/>
                            <field name="responsible_id"/>
                        </group>
                        <notebook>
                            <page string="Description">
            </field>
        </record>

        <!-- override the automatically generated list view for courses -->
        <record model="ir.ui.view" id="course_tree_view">
            <field name="name">course.tree</field>
            <field name="model">openacademy.course</field>
            <field name="arch" type="xml">
                <tree string="Course Tree">
                    <field name="name"/>
                    <field name="responsible_id"/>
                </tree>
            </field>
        </record>

        <!-- window action -->
        <!--
            The following tag is an action definition for a "window action",
                <form string="Session Form">
                    <sheet>
                        <group>
                            <group string="General">
                                <field name="course_id"/>
                                <field name="name"/>
                                <field name="instructor_id"/>
                            </group>
                            <group string="Schedule">
                                <field name="start_date"/>
                                <field name="duration"/>
                                <field name="seats"/>
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- session tree/list view -->
        <record model="ir.ui.view" id="session_tree_view">
            <field name="name">session.tree</field>
            <field name="model">openacademy.session</field>
            <field name="arch" type="xml">
                <tree string="Session Tree">
                    <field name="name"/>
                    <field name="course_id"/>
                </tree>
            </field>
        </record>

        <record model="ir.actions.act_window" id="session_list_action">
            <field name="name">Sessions</field>
            <field name="res_model">openacademy.session</field>

练习逆关联One2many
使用逆关联字段one2many,编辑模型以反映课程和授课之间的关系。

openacademy/models.py

    responsible_id = fields.Many2one('res.users',
        ondelete='set null', string="Responsible", index=True)
    session_ids = fields.One2many(
        'openacademy.session', 'course_id', string="Sessions")


class Session(models.Model):

openacademy/views/openacademy.xml

                            <page string="Description">
                                <field name="description"/>
                            </page>
                            <page string="Sessions">
                                <field name="session_ids">
                                    <tree string="Registered sessions">
                                        <field name="name"/>
                                        <field name="instructor_id"/>
                                    </tree>
                                </field>
                            </page>
                        </notebook>
                    </sheet>

练习多对多关联many2many
在授课模型中添加关联字段many2many,将每次授课和参与的听众做关联,听众来自于内置模型res.partner。相应的调整对应的视图。

openacademy/models.py

    instructor_id = fields.Many2one('res.partner', string="Instructor")
    course_id = fields.Many2one('openacademy.course',
        ondelete='cascade', string="Course", required=True)
    attendee_ids = fields.Many2many('res.partner', string="Attendees")

openacademy/views/openacademy.xml

                                <field name="seats"/>
                            </group>
                        </group>
                        <label for="attendee_ids"/>
                        <field name="attendee_ids"/>
                    </sheet>
                </form>
            </field>
上一篇 下一篇

猜你喜欢

热点阅读