How to change the attributes of fields in an XML form view according to a condition from the model's python code?
let's say I have this python code:
class SomeClass(models.Model):and this is the form view:
_name = "some.name"
_inherit = [
'some_model.mixin',
]
_fields_to_protect = fields.Many2many(
comodel_name="_model_to_protect",
domain="[('model_id', '=', _model_to_protect)]")
_fields_to_protect_list = ['first_field','second_field'] # gets calculated from the previous field
first_field = fields.Char() # we will protect this from edit in the form xml view
second_field = fields.Char() # we will protect this from edit in the form xml view
third_field = fields.Char() # this one is not affected
<record model="ir.ui.view" id="some_view_form">
<field name="model">some.name</field>
<field name="arch" type="xml">
<form>
<field name="first_field"/>
<field name="second_field"/>
<field name="third_field"/>
</form>
</record>
I'd like the fields mentioned on _fields_to_protect_list to dynamically change when viewed to something like this using any possible way:
<field name="first_field" readonly="1"/>
<field name="second_field" readonly="1"/>
I've tried many different ways, but none worked in odoo 13 such as using context or node.set(), maybe I didn't use them right or I maybe should use something else.
I'm working on my first mixin addon, it protects fields according to the current selected many2one class, field.Selection, user, or group in a record that can be assigned from the client, they can be read but not write, I've done the security part from python that overrides the write() and raises an exception for the record that inherits the mixin, what is left is making the fields readonly in the xml view. When it's done, I will publish it on Github.