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

I want to apply the readonly attribute to all fields of the form if a certain condition is met (i.e. when a field has a certain value). 

I would be able to achieve this if I hardcoded all the fields in the form with this:-

attrs="{'readonly': [('stage', '=', 'Freeze')]}"

But I don't wish to do this. This is not an elegant solution and kind of a brittle implementation.

I would like to have an automatic solution. Is there a built-in method that I can overwrite and apply this readonly attribute for all fields irrespective of the fields in the form?

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

Hi

You should inherit the fields_view_get method

Try this:

    @api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        result = super(SaleOder, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(result['arch'])
        if view_type == 'form':
            for node in doc.xpath("//field"):
                domain = [['stage', '=', 'Freeze']]
                if node.attrib.get('modifiers'):
                    attr = json.loads(node.attrib.get('modifiers'))
                    if attr.get('readonly'):
                        value_readonly = attr.get('readonly')
                        if str(attr.get('readonly')) != "True":
                            value_readonly.insert(0, "|")
                            domain = value_readonly + domain
                    attr['readonly'] = domain
                    node.set('modifiers', json.dumps(attr))
        result['arch'] = etree.tostring(doc)
        return result
Hope it helps!!

Ảnh đại diện
Huỷ bỏ
Tác giả

This worked wonderfully and was easy to understand. Thank you!

You're welcome

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 10 24
1636
1
thg 5 24
2590
1
thg 11 22
4925
2
thg 7 22
3660
9
thg 7 21
62814