关注各种黑科技
B站UP主,编程教学,游戏玩家,挂B,金牛座

Odoo Wizards向导

Wizards向导   在odoo中通过使用向导创建一个表单与用户进行交互。向导的model使用TransientModel定义,TransientModel继承自Model,并有下面的特殊功能 1 保存在TransientModel的记录会在提交完成后自动清理 2 wizard记录没有必要指定访问权限,默认所有用户都可以访问。 3 wizard记录中的字段可以通过many2one指向wizard对象或普通的model对象, 普通的model对象不能通过many2one指向wizard对象 下面我们将创建wizward供用户来快速添加上课学生到课时中。   练习 定义一个wizard,让用户批量的添加某节课时的上课学生 openacademy/wizard.py
from openerp import models, fields, api
class Wizard(models.TransientModel):
    _name = 'openacademy.wizard'
    session_id = fields.Many2one('openacademy.session',
        string="Session", required=True)
    attendee_ids = fields.Many2many('res.partner', string="Attendees")
  openacademy/wizard.py 需要导入到openacademy/__init__.py
from . import wizard
  运行wizard 可以通过ir.actions.act_window模型表里的记录发起,可以从menu里或者通过某个button触发。 另外还有一种方式,在form view上方的下拉按钮组(contextual actions)中调用,设置好src_model关联 对应的model。
<act_window id="launch_the_wizard"
            name="Launch the Wizard"
            src_model="context.model.name"
            res_model="wizard.model.name"
            view_mode="form"
            target="new"
            key2="client_action_multi"/>
    练习 启动向导 1 定义向导显示的form视图 2 在contextual actions中调用此向导 3 定义向导模型中session_id的默认值,通过self._context中active_id获取 openacademy/wizard.py
class Wizard(models.TransientModel):
    _name = 'openacademy.wizard'
    def _default_session(self):
        return self.env['openacademy.session'].browse(self._context.get('active_id'))
    session_id = fields.Many2one('openacademy.session',
        string="Session", required=True, default=_default_session)
    attendee_ids = fields.Many2many('res.partner', string="Attendees")
    @api.multi
    def subscribe(self):
        self.session_id.attendee_ids |= self.attendee_ids
        return {}
  openacademy/views/openacademy.xml
                  parent="openacademy_menu"
                  action="session_list_action"/>
        <record model="ir.ui.view" id="wizard_form_view">
            <field name="name">wizard.form</field>
            <field name="model">openacademy.wizard</field>
            <field name="arch" type="xml">
                <form string="Add Attendees">
                    <group>
                        <field name="session_id"/>
                        <field name="attendee_ids"/>
                    </group>
                    <footer>
                         <button name="subscribe" type="object" string="Subscribe" class="oe_highlight"/>                or <button special="cancel" string="Cancel"/>
                     </footer>
                </form>
            </field>
        </record>
        <act_window id="launch_session_wizard"
                    name="Add Attendees"
                    src_model="openacademy.session"
                    res_model="openacademy.wizard"
                    view_mode="form"
                    target="new"
                    key2="client_action_multi"/>
    </data>
</openerp>
  练习 完善上面的wizard,在一个wizard里操作多个课时。   openacademy/views/openacademy.xml
                <form string="Add Attendees">
                    <group>
                        <field name="session_ids"/>
                        <field name="attendee_ids"/>
                    </group>
                    <footer>
                        <button name="subscribe" type="object"
openacademy/wizard.py
class Wizard(models.TransientModel):
    _name = 'openacademy.wizard'
    def _default_sessions(self):
        return self.env['openacademy.session'].browse(self._context.get('active_ids'))
    session_ids = fields.Many2many('openacademy.session',
        string="Sessions", required=True, default=_default_sessions)
    attendee_ids = fields.Many2many('res.partner', string="Attendees")
    @api.multi
    def subscribe(self):
        for session in self.session_ids:
            session.attendee_ids |= self.attendee_ids
        return {}
@api.multi 返回None即可   https://www.odoo.com/documentation/8.0/howtos/backend.html#wizards
转载请注明出处
分享到: 更多 (0)

评论 0