问题
I'm having some issues with an Odoo Wizard in a list view that would take a value from the selected (checked) list items to update another record. For instance, I added a column to res_partner called related_partner_id. Basically, it's used to classify master accounts (partners). When adding a new customer, I have setup a dropdown that I could assign the master account. The database updates the related_partner_id to the child account's id from res_partner.
Here is the view I'm using.
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!--Wizard view to assign new master account-->
<record model="ir.ui.view" id="view_master_wizard_form">
<field name="name">master.wizard.form</field>
<field name="model">master.wizard</field>
<field name="arch" type="xml">
<form string="Assign New Parent">
<separator colspan="4" string="Update Parent"/>
<newline/>
<field name="related_partner_id"/>
<field name="parent_id" invisible="1"/>
<group col="4" colspan="4">
<button icon="gtk-cancel" special="cancel" string="Cancel"/>
<button icon="gtk-ok" name="change_master" string="Update" type="object"/>
</group>
</form>
</field>
</record>
<!--Add option to More dropdown in customer list viewt-->
<act_window id="launch_res_partner" name="Assign New Master"
src_model="res.partner"
res_model="master.wizard"
view_mode="form"
view_type="form"
target="new"
key2="client_action_multi"/>
<!--Action to change selected customers to the new master account-->
<record id="action_change_master" model="ir.actions.act_window">
<field name="name">Assign New Master Account</field>
<field name="type">ir.actions.act_window</field>
<field name="src_model">res.partner</field>
<field name="res_model">master.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
I followed/based it off the Odoo Wizard guide here. I can see it write entries the master_wizard table in the database (though only one no matter how many I check), but it doesn't update the res_partner table.
回答1:
Here is the model class that works with the above XML. Just need to loop through the ids and set the value from the self value to the wizard to the res.partner table. This is all the new API. It may need to be touched up a bit, but works ok for now. I had to set the parent_id as that needed to change as well.
from openerp import models, fields, api
class master_wizard(models.TransientModel):
_name = 'master.wizard'
related_partner_id = fields.Many2one('res.partner', 'Master Account')
parent_id = fields.Many2one('res.partner')
related_partner_ids = fields.One2many('res.partner', 'related_partner_id')
@api.multi
def change_master(self):
related_partner_ids = self.env['res.partner'].browse(self._context.get('active_ids'))
for partner in related_partner_ids:
partner.related_partner_id = self.related_partner_id
partner.parent_id = self.related_partner_id
return {}
来源:https://stackoverflow.com/questions/29442703/odoo-8-openerp-setting-values-using-wizard-from-list-view