如何在odoo中使用onchange检索值?

时间:2015-07-06 09:54:36

标签: python odoo

我有以下两个模块。在以下 asset1_inspection 类中,我创建了两个树视图。 'msr_insp':fields.one2many('results.measure','insp_msr','检查措施'),。我将为每次检查制定一些措施。

feedback.py 之后,当我在'insp_msr1'中选择特定检查时,我曾经为该检查获得指定的度量

config.py

 class asset1_inspection(osv.Model):
        _name = "asset1.inspection"
        _rec_name="inspection_name"

    MAINTENANCE_SELECTION=[
        ('0','Daily'),
        ('1','Weekly'),
        ('2','Fortnight'),
        ('3','Bi-Monthly'),
        ('4','Quarterly'),
        ('5','Half-Yearly'),
        ('6','Yearly'),
        ('7','Bi-Yearly'),

    ]
    MAINTENANCE_TYPE=[
        ('0', 'Corrective'),
        ('1', 'Preventive'),
        ('2', 'Predictive'),

    ]
    SHOUTDOWN_SELECTION=[
        ('0','YES'),
        ('1','NO'),

    ]
    _columns = {

        'inspection_name':fields.char('Inspection Type',),
        'freq_sel':fields.selection(MAINTENANCE_SELECTION,'Frequency'),

        'shut_down':fields.selection(SHOUTDOWN_SELECTION,'Shout Down'),
        'main_type':fields.selection(MAINTENANCE_TYPE,'Maintenance Type',),
        'insp_id1' : fields.one2many('inspec1.grid','insp_id','BoM',),
        'msr_insp' : fields.one2many('results.measure','insp_msr','Inspection Measure',),


    }

asset1_inspection()

class inspec1_grid(osv.Model):
    _name = 'inspec1.grid'
    _columns = { 
                'insp_name' : fields.char('Part'),
                'insp_code' : fields.char('Code'),
                'insp_quty' : fields.char('Quantity '),
                'insp_uom' : fields.char('UoM'),
                'insp_id': fields.many2one('asset1.inspection','Insp Id'),
            }

inspec1_grid()

class results_measure(osv.Model):
    _name = 'results.measure'
    _rec_name='measure'
    _columns = {
            'measure' : fields.char('Inspection Measure'),
            'insp_msr' : fields.many2one('asset1.inspection','Insp Measr'),
        }

results_measure()
**feedback.py**


     from openerp.osv import fields, osv
        from openerp import api

    class feedback_form(osv.Model):
        _name = 'feedback.form'
        _rec_name = 'inspec_type'
        _columns={
            'location' : fields.char('Substation Location'),

            'insp_rslt' : fields.one2many('feedback.tree','insp_rsltts','Inspection Result',),
        }





    class feedback_tree(osv.Model):
        _name = 'feedback.tree'
        _columns = {
            'insp_type' : fields.many2one('asset1.inspection','Inspection Type',),
            'frequency' : fields.char('Frequency'),
            'shutdown' : fields.char('Shout Down'),
            'insp_msr1' : fields.many2one('results.measure','Result',),
            'insp_rsltts': fields.many2one('feedback.form','Result Id'),
        }

        def get_inspection(self, cr, uid, ids, inspec_type, context=None):
            val = {}
            if inspec_type:
                for det in self.pool.get('asset1.inspection').browse(cr,uid,inspec_type,context=context):
                        val = {
                            'insp_msr1' : det.measure,
                            #'qty' : det.qty,
                            #'uom' : det.uom,
                            }
            return {'value': val}

1 个答案:

答案 0 :(得分:0)

要使用on_change,您必须转到视图,然后转到要更改的字段。

例如:

<field name="my_field" on_change="onchange_method(xx_field_a, xx_field_b)"/>

因此,在上面的示例中,此字段将在更改时调用方法“onchange_method”。您还可以为其提供包含表单上当前值的参数。这就是检索这些值的方法。

onchange_method本身非常简单:

def onchange_method(self, cr, uid, ids, xx_field_a, xx_field_b, context=None):
    return {'value': {'my_field': xx_field_a * xx_field_b}}

在这里你可以看到我从表单中检索了xx_field_a和xx_field_b,因为我在视图的on_change中将它们作为参数传递。