تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
3344 أدوات العرض

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?

الصورة الرمزية
إهمال
أفضل إجابة

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

الصورة الرمزية
إهمال
الكاتب

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

You're welcome

المنشورات ذات الصلة الردود أدوات العرض النشاط
1
أكتوبر 24
1719
1
مايو 24
2602
1
نوفمبر 22
4936
2
يوليو 22
3683
9
يوليو 21
62830