Odoo: ValueError: Invalid field 'user_id' in leaf

Deadly 提交于 2021-02-08 10:14:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!