问题
I am trying to create rule, to limit users to see only their records in tree view in my module, but it gives me error that 'user_id' field doesn't exist...
Heres my model:
class help_desk(models.Model):
_name = 'help_desk.help_desk'
name = fields.Char(string="Title", required=True)
number = fields.Char(string="Mobile", required=True)
Here's the rule:
<record model="ir.rule" id="per_user_rule">
<field name="name">User: see the individual Record</field>
<field name="model_id" ref="model_help_desk_help_desk"/>
<field name="domain_force">['|',('user_id','=',False),('user_id','=',user.id)]</field>
<field name="groups" eval="[(4,ref('base.group_user'))]"/>
</record>
Here's the Error:
ValueError: Invalid field 'user_id' in leaf "<osv.ExtendedLeaf: ('user_id', '=', 9) on help_desk_help_desk (ctx: )>"
I am guessing, i have to add user_id field to my model, but how can i relate it to current user?
回答1:
I found solution. I added following field to my model:
user_id = fields.Many2one('res.users', string='User', track_visibility='onchange', readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.user)
it takes current user as default value.
回答2:
Change user_id to create_uid
create_uid - defines user who created the record
<field name="domain_force">[('create_uid','=',uid)]</field>
回答3:
.py file class help_desk(models.Model): _name = 'help_desk.help_desk'
name = fields.Char(string="Title", required=True)
number = fields.Char(string="Mobile", required=True)
user_id = fields.Many2one('res.users', string='User', track_visibility='onchange', readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.user)
security/.xml file (ir.rule)
<record model="ir.rule" id="per_user_rule">
<field name="name">User: see the individual Record</field>
<field name="model_id" ref="model_help_desk_help_desk"/>
<field name="domain_force">['|',('user_id','=',False),('user_id','=',user.id)]</field>
<field name="groups" eval="[(4,ref('base.group_user'))]"/>
</record>
来源:https://stackoverflow.com/questions/36444081/odoo-valueerror-invalid-field-user-id-in-leaf