Odoo Help


How can I use domain filter with a function to hide lines in a treeview in odoo 9?

Baintex Technologies S.L.
on 9/22/16, 6:39 AM 1,332 views

I have an animals model, a cage model and a relational model between users(workers) and cages:


class animal(osv.osv):
 _name = 'animal'
 _columns = {
 'name': fields.char('Animal Name', size=100),
 'cage_id': fields.many2one('cage', required=True, ondelete='cascade', string="Cage")

class cage(osv.osv):
 _name = 'cage'
 _columns = {
 'name': fields.char('Cage Name', size=100),
 'animals': fields.one2many('animal', 'cage_id', string="Animals"),
 'workers': fields.one2many('cage.user.relation', 'cage_id', string="Workers")

class cage_user_relation(osv.osv):
 _name = 'cage.user.rel'
 _columns = {
 'cage_id': fields.many2one('cage', required=True, ondelete='cascade', string="Cage"),
 'user_id': fields.many2one('res.users', required=True, ondelete='cascade', string="Worker"),

I have this in the cage_manager.xml:

<record id="view_cage_manager_tree" model="ir.ui.view">
 <field name="name">cage.tree</field>
 <field name="model">cage</field>
 <field name="type">tree</field>
 <field name="arch" type="xml">
 <tree string="cage_tree">
 <field name="name"/>
 <field name="animals"/>
 <field name="workers"/>

I want to allow each worker to see only the cages associated to him. (The administrator can see every cage)

I want something like this:

<record id='action_menu_cage_manager' model='ir.actions.act_window'>
 <field name="name">Cages Manager</field>
 <field name="res_model">cage</field>
 <field name="view_type">form</field>
 <field name="view_mode">tree,form</field>
 <field name="domain">[
 ('id', 'in', 'cages_of_current_worker()')

But I don't know how and where to implement the cages_of_current_worker() function

Martin Varela
On 9/22/16, 2:56 PM

Hi, how about using a security rule?

And why are you using a one2many relation to 'cage.user.relation' when you can define a many2many to res.users? I think the model cage.user.rel is not necessary in this case.

Hi, Martin

The security rule is okay if you have a few "cages", but If you have hundreds, it will be a tedious option.

I need a one2many because I want to put more fields inside the relational table.

Baintex Technologies S.L.
on 9/23/16, 3:19 AM

Ok, it makes sense. Why you think the security rule is tedious?

Define a computed field 'user_ids' in 'cage' (it would be like cages_of_current_worker), and a security rule with domain [('user_ids','in',[user.id])]

Martin Varela
on 9/23/16, 9:19 AM

That's exactly that I want to do, but I don't know how to implement a security rule with a computed field.

Baintex Technologies S.L.
on 9/23/16, 11:00 AM
Divyang Jariwala
On 9/22/16, 7:56 AM

Hi MouTio,

in that case server action may help you.

reference : \ir.actions.server 

Hi Divyang.

I don't know anything about server action. How can it help me?

Baintex Technologies S.L.
on 9/23/16, 4:25 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)


Asked: 9/22/16, 6:39 AM
Seen: 1332 times
Last updated: 9/22/16, 2:56 PM