在Odoo-8中Onchange时,树视图字段不会更新

时间:2016-06-01 06:10:18

标签: python xml openerp onchange odoo-8

这是我的Python代码。它继承了hr.employee,res.partner和sale.order。

from openerp.osv import fields, osv

#Inheriting the hr.employee object
class hr_employee(osv.osv):
    _inherit = "hr.employee"

    _columns = {
                'customer_ids': fields.many2many('res.partner', 'lead_employee_rel', 'emp_id', 'partner_id', 'Customers'),
                'rating_ids':fields.one2many('hr.employee','rating_id',"Employee Rating"),
                }

#Inheriting the res.partner object
class res_partner(osv.osv):
    _inherit ="res.partner"

    _columns = {
                #creating many2many functionality
                'partner_id' : fields.integer('ID'),
                'employee_ids': fields.many2many('hr.employee', 'lead_employee_rel', 'partner_id', 'emp_id', 'Employees'),
    }

#Inheriting sale.order object
class sale_order(osv.osv):
    _inherit = "sale.order"
    _description = "Sales Order"

    _columns = {
                'employee_ids': fields.many2many('hr.employee', 'lead_employee_rel', 'partner_id', 'emp_id', 'Employees'),
                'rating_id':fields.many2one('hr.employee',"Employee Rating"),
                'emp_select':fields.selection([
                                             ('auto', 'Auto'),
                                             ('manual', 'Manual'),
                                             ], string='Employee Selection Mode',index=True),
    }

#create method for allotting the employees to the client
    def create(self, cr, uid, values, context=None):
        partner_id=values.get('partner_id')
        if partner_id:
            emp_ids=values.get('employee_ids')
            for line in emp_ids:
                self.pool.get('res.partner').write(cr, uid, partner_id, {'employee_ids': [(4, line[2])]}, context=context)
        return super(sale_order, self).create(cr, uid, values, context=context)
        #write method for updating the employees list

    def write(self, cr, uid, ids, vals, context=None):
        partner_id=vals.get('partner_id')
        quotation_obj=self.browse(cr, uid, ids,context=context)
        if not partner_id:
            #if partner id not found, find the employee ids
            emp_ids=vals.get('employee_ids')
            if emp_ids:
#                 for line in emp_ids:
#                     employee_ids=line[2]
#             else:#update the employee ids
#                 employee_ids=quotation_obj.employee_ids
                for line in emp_ids:
                    self.pool.get('res.partner').write(cr, uid, quotation_obj.partner_id.id, {'employee_ids': [(4, line[2])]}, context=context)

        if partner_id:
            emp_ids=vals.get('employee_ids')
            if emp_ids:
                for line in emp_ids:
                    self.pool.get('res.partner').write(cr, uid, partner_id, {'employee_ids': [(4, line[2])]}, context=context)
        return super(sale_order, self).write(cr, uid, ids, vals, context=context)
#load the employees while onchanging the customer (inherited from sale.order)

    def onchange_partner_id(self, cr, uid, ids, part, emp_select, context=None):
        if not part:
            return {'value': {'partner_invoice_id': False, 'partner_shipping_id': False,  'payment_term': False, 'fiscal_position': False}}

        part = self.pool.get('res.partner').browse(cr, uid, part, context=context)
        addr = self.pool.get('res.partner').address_get(cr, uid, [part.id], ['delivery', 'invoice', 'contact'])
        pricelist = part.property_product_pricelist and part.property_product_pricelist.id or False
        invoice_part = self.pool.get('res.partner').browse(cr, uid, addr['invoice'], context=context)
        payment_term = invoice_part.property_payment_term and invoice_part.property_payment_term.id or False
        dedicated_salesman = part.user_id and part.user_id.id or uid
        val = {
            'partner_invoice_id': addr['invoice'],
            'partner_shipping_id': addr['delivery'],
            'payment_term': payment_term,
            'user_id': dedicated_salesman,
        }
#loading the alloted employee list from res.partner while changing the customer
        emp_ids=[]
        for line in part.employee_ids:
            emp_ids.append(line.id)
            val.update({'employee_ids':emp_ids})

        delivery_onchange = self.onchange_delivery_id(cr, uid, ids, False, part.id, addr['delivery'], False,  context=context)
        val.update(delivery_onchange['value'])
        if pricelist:
            val['pricelist_id'] = pricelist
        if not self._get_default_section_id(cr, uid, context=context) and part.section_id:
            val['section_id'] = part.section_id.id
        sale_note = self.get_salenote(cr, uid, ids, part.id, context=context)
        if sale_note: val.update({'note': sale_note})
        return {'value': val}

这是我的XML代码

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="view_order_form_inherited">
            <field name="name">sale.order.form.inherit</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form" />
            <field name="arch" type="xml">
                <xpath expr="//field[@name='partner_id']" position="before">
                    <field name="emp_select" widget="radio" required="1" />
                </xpath>
                <xpath expr="//field[@name='partner_id']" position="replace">
                    <field name="partner_id"
                        on_change="onchange_partner_id(partner_id, emp_select, context)"
                        domain="[('customer','=',True)]" context="{'search_default_customer':1, 'show_address': 1}"
                        options='{"always_reload": True}' />
                </xpath>
                <xpath expr="//form/sheet/notebook/page[@string='Order Lines']"
                    position="after">
                    <page name="employees" string="Employee Allocation">
                        <field name="employee_ids" widget="many2many"
                            attrs="{'readonly': [('emp_select','=', 'auto')]}">
                            <tree> <field name="name" /> <field name="rating_id" /> </tree>
                        </field>
                    </page>
                </xpath>
            </field>
        </record>
    </data>
</openerp>

在更改customer_id时,页面必须在树视图中仅使用两个字段(员工姓名和员工评级)加载员工详细信息。这些字段是第一次正确加载,但是当我更改客户ID时,树视图字段没有更新。

1 个答案:

答案 0 :(得分:0)

尝试使用openerp.osv.orm.BaseModel(V7)或openerp.models.Model(V8 +)中def write下记录的“幻数”:

def on_change_partner_id(self, ...):
    # ...
    emp_tuple_list=[(5,)] # without (5,) it won't delete all old data
    for emp in part.employee_ids:
        emp_tuple_list.append((4, emp.id))
    val.update({'employee_ids': emp_tuple_list})
    # ...