콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
10945 화면

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
20064
2
7월 20
3801
0
12월 19
4453
1
6월 24
2787