计算字段自己的类和不同的类? - Openerp

时间:2015-09-27 15:11:52

标签: python function arguments openerp self

我的代码有问题。我希望能得到一些帮助。

class demo(osv.osv):

    _inherit = 'compresores.datos'

    def _buscar_compresor(self, cr, uid, ids, context=None):
        auxpower = self.browse(cr, uid, id).power
        obj = self.pool.get('compresores.datos')
        ids = obj.search(cr, uid, [])
        res = obj.read(cr, uid, ids, ['name', 'id', 'comp_a1','comp_a2'], context)
        res = [(r['id'], r['name'], r['comp_a1'],r['comp_a2']) 
        for r in res 
            if (r['comp_a1'] >= auxpower)]
        return res

    _name = "demo_datos"
    _description = "Demo"
    _columns = {
        'power': fields.float('Potencia frigorifica',digits=(3,2)),
        'compresor1': fields.selection(_buscar_compresor,method="True", type="char", size=256, string="Compresor" ),

    }

demo()

这是XML

        <record model="ir.ui.view" id="demo_form">
            <field name="name">demo.form</field>
            <field name="model">demo_datos</field>
            <field name="arch" type="xml">
                <form string="DEMO" version="7.0">
                    <group col='4' colspan='2'>
                        <field name="power" class="oe_inline" />
                        <field name="compresor1" />
                     </group>
                </form>
            </field>
        </record>

        <record model="ir.ui.view" id="demo_tree">
            <field name="name">demo.tree</field>
            <field name="model">demo_datos</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="DEMO general">
                    <field name="power" />
                </tree>
            </field>
        </record>

我班上有power字段。我需要一个函数来比较另一个类(comp_a1)中的字段是否更高并返回该类中的寄存器。

我使用self.pool.get来调用另一个类,它可以正常工作。但是,当我使用self.browse在相同的函数中读取字段power(使用auxpower)时,我认为存在参数问题......我该怎么办?

2 个答案:

答案 0 :(得分:1)

<强> fields.selection

允许用户在各种预定义值之间进行选择的字段。

语法:

fields.selection([('value','display'), ('value','display')],
                   'Title' [, Optional Parameters]),

选择参数的格式:表格的字符串元组列表:

[('value', 'display'),('value', 'display'),('value', 'display')]

注意

_inherit = 'compresores.datos'
_name = "demo_datos"

如果您想扩展/更新现有模型的功能,那么您可以使用 _inherit ,但 _name _inherit 将被创建为新model _name ,其中包含 _inherit 模型的所有功能。

您应该按照以下方式更新您的代码,

class demo(osv.osv):

    _inherit = 'compresores.datos'

    def _buscar_compresor(self, cr, uid, ids, context=None):
        obj_comp = self.browse(cr, uid, ids[0], context=context)
        if obj_comp:
            auxpower = obj.power
        obj = self.pool.get('compresores.datos')
        ids = obj.search(cr, uid, [])
        res = obj.read(cr, uid, ids, ['name', 'id', 'comp_a1','comp_a2'], context)
        dict = [(r['id'], r['name']) for r in res if (r['comp_a1'] >= auxpower)]
        return dict

    _columns = {
        'power': fields.float('Potencia frigorifica',digits=(3,2)),
        'compresor1': fields.selection(_buscar_compresor,method="True", type="char", size=256, string="Compresor" ),

    }

demo()

答案 1 :(得分:0)

感谢您的帮助。我现在使用这段代码,目前正在使用它。

def _buscar_compresor(self, cr, uid, ids, power, arg, context=None):
    aux1={}
    for record in self.browse(cr, uid, ids):
        aux1= record.power
    records = self.browse(cr, uid, ids)
    obj = self.pool.get('compresores.datos')
    ids = obj.search(cr, uid, [])
    res3 = obj.read(cr, uid, ids, ['nombre', 'id', 'comp_a1'], context)
    res3 = [(str(r['nombre']), str(r['tipo_gas']), int(r['comp_a1']) 
    for r in res3 
        if (r['comp_a1'] >= aux1 )]
    resultado = {}
    for r in records:
        resultado[r.id] = str(res3) 
    return resultado