试图更好地理解orm方法,但我可以在一个字段中存储一个值

时间:2015-04-14 15:55:59

标签: openerp openerp-7 odoo openerp-8 odoo-8

我想存储一个字段的价值' por_sucursal'来自字段中的res_users类' probando'从pruebas的值,它的布尔值,我可以存储它,我需要它为xml文件中的过滤器。

class pruebas(osv.Model):
    _name = 'pruebas'
    _description = 'Sucursales de la empresa'  


def dame_usuario2(self, cr, uid, ids, field_names=None, arg=False, context=None):
    if context is None:
        context = {}
    var_pool = self.pool.get('res_users')
    if var_pool:
        ids = var_pool.search(cr, uid, [('id','=',uid)])
        if ids:
            return ids[0].por_sucursal


_columns = {            
        'name': fields.char('prueba', size=50, help="Probando"),
        'probando': fields.function(dame_usuario2, type='boolean', store=True,
                                            readonly = True),
            }
pruebas()

2 个答案:

答案 0 :(得分:0)

尝试以下,

class pruebas(osv.Model):
    _name = 'pruebas'
    _description = 'Sucursales de la empresa'  


    def dame_usuario2(self, cr, uid, ids, field_names=None, arg=False, context=None):
        if context is None:
            context = {}
        var_pool = self.pool.get('res.users')
        res = {}
        for obj in self.browse(cr, uid, ids, context=context):
            res[obj.id] = False
            user = var_pool.browse(cr, uid, uid, context=context)
            if user:
                res[obj.id] = user.por_sucursal
        return res

    _columns = {            
            'name': fields.char('prueba', size=50, help="Probando"),
            'probando': fields.function(dame_usuario2, type='boolean', store=True, readonly = True),
                }
pruebas()

答案 1 :(得分:0)

功能字段的签名是:

fnct: function to compute the field value (required)

    def fnct(self, cr, uid, ids, field_name, arg, context)
        returns a dictionary { ids→values } with values of type=type

查看方法的返回类型dictionary { ids→values }。您需要返回字典字典,例如{ids: {'key':'value'}}key -> field name

class pruebas(osv.Model):
    _name = 'pruebas'
    _description = 'Sucursales de la empresa'  

    def dame_usuario2(self, cr, uid, ids, field_names=None, arg=False, context=None):
        result = {}
        if context is None:
            context = {}
        var_pool = self.pool.get('res.users')
        for obj in self.browse(cr, uid, ids, context=context):
            value = {}
            user = var_pool.browse(cr, uid, uid, context=context)
            if user:
                value['probando'] = user.por_sucursal
                result[obj.id] = value
        return result

    _columns = {            
            'name': fields.char('prueba', size=50, help="Probando"),
            'probando': fields.function(dame_usuario2, type='boolean', store=True, readonly = True),
                }
pruebas()