Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
10939 Lượt xem

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.



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 2 23
20046
2
thg 7 20
3798
0
thg 12 19
4444
1
thg 6 24
2786