コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
10960 ビュー

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):
_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
 and this is the form view:

     <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.



アバター
破棄
最善の回答

Hi,

For changing the attributes of a field in XML through python code, you can use fields_view_get

For reference you can follow the link below which making all fields in a form readonly using the fields view get
https://www.odoo.com/forum/help-1/question/is-there-any-way-that-i-can-make-a-entire-form-readonly-132701

Also refer following forum posts

https://stackoverflow.com/questions/54824849/what-is-the-fields-view-get-option-in-the-odoo-11-developer-menu

https://www.odoo.com/forum/help-1/tag/fields-view-get-1190/questions

Regards

アバター
破棄
関連投稿 返信 ビュー 活動
1
2月 23
20102
2
7月 20
3806
0
12月 19
4479
1
6月 24
2793