Odoo Help


Setting another field to read only in onchange event - NewAPI

Graham Vickrage
on 9/2/17, 1:17 PM 822 views

Odoo V8

I've added a global discount field that I want that I only want editable if the Public Pricelist is selected, unless the user is in the sale manager group, in which case they can edit irrespective of the pricelist selected.


class sale_order(models.Model):
_inherit = 'sale.order'
    ct_global_discount = fields.Float('Global Discount')
    can_apply_discount = fields.Boolean(default=False)

def onchange_pricelist_id(self):
    if self.pricelist_id.name != 'Public Pricelist':
        self.ct_global_discount = 0
        if self.env.user.has_group('base.group_sale_manager'):
            self.can_apply_discount = True
            self.can_apply_discount = False
        self.can_apply_discount = True


<xpath expr="//group[@name='sale_total']" position="after">
<field name="can_apply_discount" invisible="1"/>
<field name="ct_global_discount" attrs="{'readonly': ['can_apply_discount', '=', False]}" />

I get an error that can_apply_discount does not exist. I can get this working using the old interface but can't for the life of me see how you get this working with the new API.

addition: I've also tried without @api.one but with no luck

Graham Vickrage
on 9/2/17, 1:23 PM
Yogendrasinh Chavda
On 9/4/17, 2:34 AM

Hello Graham Vickrage,

I have seen your scenario you gave attrs on " ct_global_discount " field its syntax is actually wrong.
its should be like this :

<field name="ct_global_discount" attrs="{'readonly':[('can_apply_discount', '=', False)]}"/>

and also small correction, when you add new fields through .py file, must add a label (field string).

can_apply_discount = fields.Boolean(string="Can Apply Discount?" default=False)   


About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 9/2/17, 1:17 PM
Seen: 822 times
Last updated: 9/4/17, 8:56 AM