Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
3366 Widoki

Hi,
How can I make many2one field as read only in onchange functionality of odoo?. Please suggest where i am wrong in below code.

def onchange_product_id(self, cr, uid, ids, product_id=None, size_id=None, context={}):    
size_id = fields.many2one('ring.size', 'Ring size')
tmpl_id = self.pool.get('product.product').browse(cr, uid, product_id).product_tmpl_id.id

if tmpl_id:
sql = """SELECT * FROM product_ring_size_default_rel
WHERE product_id = {}""".format(tmpl_id)

cr.execute(sql)
if not cr.fetchall():
size_id = fields.many2one('ring.size', 'Ring size', readonly=True)
Awatar
Odrzuć

Please specify the form which field to which field you want to readyonly

Explain it bit properly not only with single method (specify model and its related field in xml )

Najlepsza odpowiedź

Hello,

You may try this,

First take one boolean field and use that field in your onchange function.It will True if your condition followed accordingly. then add attrs in your field in xml file which make your field readonly.

For Ex.

#add one boolean field
'boolean_field':fields.boolean('Boolean Field'),

#update your onchange function
def onchange_product_id(self, cr, uid, ids, product_id=None, size_id=None, context={}):
    res = {'value':{'boolean_field':False}}
    tmpl_id = self.pool.get('product.product').browse(cr, uid, product_id).product_tmpl_id.id

    if tmpl_id:
        sql = """SELECT * FROM product_ring_size_default_rel
                   WHERE product_id = {}""".format(tmpl_id)

        cr.execute(sql)
        if not cr.fetchall():
            res = {'value':{'boolean_field':True}}
        return res


#add attrs in your xml file
<field name="boolean_field" />
<field name="size_id" attrs="{'readonly':[('boolean_field','=',True)]}" />


Hope it will help you,

Thanks

Awatar
Odrzuć
Autor

Thank you so much. It worked for me.