Odoo Wizard Wizard operation context and sample code

This paper describes how Odoo Wizard Wizard creates and provides sample code

Odoo direction 导 Wizard

In odoo create a form to interact with the user by using the wizard (Wizard). Model using the wizard TransientModel defined, TransientModel inherited from Model, and the following special features have
1 saved in TransientModel records will automatically clean up after the submission of the completion of
2 wizard records are not necessary to specify access rights by default all users can access.
Field in the record. 3 wizard wizard can point to an object or objects by a common model many2one, conventional model object can not many2one objects pointing wizard

The sample code

The definition of a wizard, allowing users to add a batch of school students at a lesson

odoogoedu/wizard.py

from odoo import models, fields, api

class Wizard(models.TransientModel):
_name = 'odoogoedu.wizard'

session_id = fields.Many2one('odoogoedu.session',
string="Session", required=True)
attendee_ids = fields.Many2many('res.partner', string="Attendees")
新建的odoogoedu/wizard.py 需要导入到odoogoedu/__manifest__.py

from . import wizard
运行wizard

Table model can record ir.actions.act_window initiation can be triggered from inside or through a menu button. Another way, the drop-down button set form above view (contextual action contextual actions) call, a good correlation is provided src_model corresponding model.

 

Startup Wizard

Defined guidance display form 1 view
2 contextual actions invoked wizard in the
3 to define default values in the session_id Wizard model. Self._context obtained by the active_id, active_id id as the current model form displayed by the browse method model, you can get to the object record.

odoogoedu/wizard.py

class Wizard(models.TransientModel):
_name = 'odoogoedu.wizard'

def _default_session(self):
return self.env['odoogoedu.session'].browse(self._context.get('active_id'))

session_id = fields.Many2one('odoogoedu.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 {}
odoogoedu/views/views.xml

parent="odoogoedu_menu"
action="session_list_action"/>


wizard.form
odoogoedu.wizard




or

 



Perfect wizard

Improve the above wizard, operating in more hours in a wizard.

odoogoedu/views/views.xml




odoogoedu/wizard.py

class Wizard(models.TransientModel):
_name = 'odoogoedu.wizard'

def _default_sessions(self):
return self.env['odoogoedu.session'].browse(self._context.get('active_ids'))

session_ids = fields.Many2many('odoogoedu.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即可

Reference article

Guess you like

Origin www.cnblogs.com/odoo12/p/12499283.html