Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
3359 Zobrazení

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?

Avatar
Zrušit
Nejlepší odpověď

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

Avatar
Zrušit

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>

Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
4
kvě 25
2570
2
kvě 25
5954
1
bře 25
1715
4
bře 25
4563
3
úno 25
5570