Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
7896 Переглядів

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









Аватар
Відмінити
Найкраща відповідь

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.

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])]

Автор

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

Найкраща відповідь

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?

Related Posts Відповіді Переглядів Дія
1
груд. 23
16818
0
трав. 16
3340
0
трав. 23
2698
2
січ. 23
3538
1
лист. 22
4485