跳至內容
選單
此問題已被標幟
2 回覆
3357 瀏覽次數

Hi, i want to make a field become read only based on current user login.

I have field 'name' (many2one res.users) & 'checklist' (boolean)

The name is manualy choose and the checklist field is read only if the name is not the same as user id login. Is it by python in backend / by xml enough?

頭像
捨棄
最佳答案

Hi Ricky Raymond,


In Odoo, you can achieve this requirement by using a combination of Python code and XML. You will need to define a computed field in the Python model that checks whether the current user's ID matches the value of the name field. Then, you can use this computed field in the XML view to conditionally set the readonly attribute of the checklist field.


Here's how you can implement it:


Define a computed field in the Python model to check if the current user's ID matches the name field.

Use the computed field in the XML view to conditionally set the readonly attribute of the checklist field.

Please find code in comment. 

Feel free to contact me if needed

Hope this will help you.

Thanks & Regards,
Email:   odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

頭像
捨棄

Here's a basic example of code :-

Python (in your model file, e.g., your_model.py):

from odoo import models, api

class YourModel(models.Model):
_name = 'your.model'

name = fields.Many2one('res.users', string='Name')
check_checklist = fields.Boolean(string='Checklist', store=True, compute='_compute_checklist_readonly')
checklist = fields.Boolean(string='Checklist')

@api.depends('name')
def _compute_checklist_readonly(self):
current_user = self.env.user
for record in self:
record.check_checklist = (record.name.id == current_user.id)

XML :-

<record id="view_your_model_form" model="ir.ui.view">
<field name="name">your.model.form</field>
<field name="model">your.model</field>
<field name="arch" type="xml">
<form string="Your Model Form">
<sheet>
<group>
<field name="name" widget="many2one_avatar_user"/>
<field name="check_checklist" invisible="True"/>
<field name="checklist" readonly="not check_checklist"/>
</group>
</sheet>
</form>
</field>
</record>

最佳答案

Hi,

To implement this flow, you can add a new computed field. First, add a boolean field and set it as computed. Inside the compute function, check if the user_id field matches self.env.uid. If it matches, set the field to True; otherwise, set it to False. Then, add the readonly attribute to the field in XML using the compute field. Here's an example in Python and XML:

Python:
check_access_team_id = fields.Boolean('Check Access', compute='_compute_access_team_id')

@api.depends('user_id')
def _compute_access_team_id(self):
    for rec in self:
        rec.check_access_team_id = rec.user_id.id == self.env.uid

XML:

<field name="check_access_team_id" invisible="True"/>
<field name="user_id" widget="many2one_avatar_user"/>
<field name="team_id" readonly="not check_access_team_id"/>

In this example, the `check_access_team_id` field is computed based on whether the `user_id` matches the current user (`self.env.uid`). Then, the `team_id` field is set as readonly depending on the value of `check_access_team_id`.


Hope it helps

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
4
5月 25
2570
2
5月 25
5953
1
3月 25
1715
4
3月 25
4562
3
2月 25
5569