如何使用current_user字段进行过滤?

时间:2015-09-17 18:07:21

标签: openerp openerp-7 openerp-8

我将其添加到res.users模型中:

class res_users(osv.osv):
    _inherit = 'res.users'  

    _columns = {            
        'por_sucursal': fields.boolean('Por Sucursal', help="Es por Sucursal"), 
        'sucursal_u' : fields.many2one('sucursales', 'Sucursal', help="Selecciona la sucursal"),           
    }

    _defaults = {
        'por_sucursal': False,            
    }    

res_users()

我还有模型ordenes.orden

class ordenes_orden(osv.Model):
    _name = 'ordenes.orden'
    _description = 'Ordenes de servicio'
    _columns = {            
        'sucursal' : fields.many2one('sucursales', 'Sucursal', help="Selecciona la sucursal", required= True),
    }        

ordenes_orden()

模型sucursales来自:

class sucursales(osv.Model):
    _name = 'sucursales'
    _description = 'Sucursales de la empresa'   
    _columns = {            
        'name': fields.char('Sucursal', size=50, help="Sucursal"),
    }
sucursales()

最后我还添加了这个类:

class ordenes(osv.Model):
    _name = 'ordenes.orden'
    _inherit = 'ordenes.orden'

    def dame_sucursal(self, cr, uid, ids, field, arg, context=None):
        res = {}
        for obj in self.browse(cr, uid, ids, context=context):
            res[obj.id] = False
            user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
            if user and user.sucursal_u and user.por_sucursal:
                res[obj.id] = user.sucursal_u.id

        return res

    _columns = {            
        'name_suc': fields.function(dame_sucursal, type='many2one', store= True,
                                                readonly = True, relation='sucursales')
    }    

ordenes()

所以我想过滤所有来自sucursal的命令,它们等于树视图中current_user的sucursal。它工作正常,但只是第一次。如果我更改当前用户的sucursal,则函数字段不会更新。这是我的树视图:

<record id="open_view_ordenes_sucursales" model="ir.actions.act_window">
    <field name="name">Ordenes</field>
    <field name="res_model">ordenes.orden</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="domain">[('sucursal','=','name_suc'),('state', 'not in', ('done','cancel'))]</field>
    <field name="view_id" ref="x_sucursal_view_tree"/>   
</record>

1 个答案:

答案 0 :(得分:0)

由于Odony

完美无缺

<record id="ordenes_sucursales_rule" model="ir.rule">
        <field name="name">Per sucursal rule</field>
        <field name="model_id" ref="model_ordenes_orden"/>
        <field name="groups" eval="[(4,ref('group_sucursales'))]"></field>          
        <field name="domain_force">[('sucursal','=',user.sucursal_u.id)]</field>
        <field eval="1" name="perm_write"/>
        <field eval="1" name="perm_read"/>
        <field eval="1" name="perm_create"/>                        
</record>