Odoo Help

Welcome!

This community 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.

0

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

By
MouTio
on 9/22/16, 6:39 AM 235 views

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

cage_manager.py:

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"/>
 </tree>
 </field>
</record>


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()')
 ]</field>
</record>


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









1
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.

MouTio
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.

MouTio
on 9/23/16, 11:00 AM
0
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?

MouTio
on 9/23/16, 4:25 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

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