Reputation: 51
I've had a model that can be assigned to a user. I used this to create a domain and being able to filter an create rules to restrict records to be seen: Model:
...
class Test(models.Model):
_name = 'test.model'
user_id = fields.Many2one('res.users')
...
Rule:
<record model="ir.rule" id="test_rule">
...
<field name="domain_force">[('user_id', '=', user.id)]</field>
...
That's quite a common user of the rules, the Sale module has it, at least.
Now a user can be assigned to a lot of records and the record can have a limited quantity of users, but more than one. To resolve this, I used a Many2many:
...
user_ids = fields.Many2many('res.users', 'test_user_relation', 'test_id', 'user_id)
...
With this new field, I can't use the same rule as before, as there isn't an operator as contains. I've created the inverse field in res.users model, so the user have the list of its test records and created a rule that show only the user assigned records:
...
Users(models.Model):
_inherit = 'res.users'
test_ids = fields.Many2many('test.model', 'test_user_relation', 'user_id, 'test_id')
...
<record model="ir.rule" id="test_rule">
...
<field name="domain_force">[('id', 'in', user.test_ids)]</field>
...
All this works, but eventually a user can have thousands of test records, and I'm afraid that this will slowdown the workflow of test model. An answer that I've already discarded is to have as many user fields as the record limit of users, but I need to keep be able to change this limit number, and can't really set a max number of users.
Is there a way to make the rule so it only check that the id of the user is in the list of user_ids?
Thanks, Benjamín
Upvotes: 1
Views: 2432
Reputation: 51
Oh well, after hours of searching, i found that you can concatenate fields in domains, as this:
...
<field name="force_domain">[(user_ids.id', '=', user.id)]</field>
...
And that's the answer for my problem.
Upvotes: 2