Odoo Help


How do you make a field conditionally read only?

Jeff Beidler
on 2/12/15, 4:04 PM 1,961 views

I thought I had seen a post about making a field read only if a certain condition were true, but now I can't find that post.  This is what I'm trying:

<field name="price_unit" attrs="{'readonly': ['product_id.name_template','!=','Shipping']}"/>

(This is in the sale order lines tree.)  I'm getting this error:

Error: Unknown field S in domain ["product_id.name_template","!=","Shipping"]

Do I just have a syntax problem, or am I just going about this the wrong way?

On 2/13/15, 2:50 AM

You getting error because you've used dot notation to access other fields then actually presented in the view ( you can't use: ['product_id.name_template','!=','Shipping']  you can use only: ['product_id','!=','something'] ), as this is parsed at client side you can use only those fields taht are presented in the view. So in order to get it worked, you can add to the model a relational field that points to 'product_id.name_template' than add this field as invisible field to the view and you'll be able to to use this field in the condition like this: ['name_template_related','!=','Shipping']

Ah, knowing that it's client-side helps. Thanks!

Jeff Beidler
on 2/13/15, 12:03 PM
On 2/13/15, 6:25 AM

'name_template' : fields.related('product_id','name_template', type='char', string='Name Template',store=True),

Create this related field field in the sale.order.line  .py file and make the field as invisible in the view.

Just checks:

attrs="{readonly': [('name_template','!=','Shipping')]}"

( Note:

In v8 you can use this:

name_template= fields.Char(related='product_id.name_template', store=True, readonly=True) )

This can help you

Thanks. Looks like 'Char' shouldn't be capitalized for v8, though.

Jeff Beidler
on 2/13/15, 12:04 PM
On 2/12/15, 5:37 PM

Try this:


Well, I get a different error now, so I suppose that's progress. It's a field not found error, so I guess I just have to figure out how to get the product name from the sales order line. The product_id field links to the product.product table, so why can't it evaluate the name_template field value?

Jeff Beidler
on 2/12/15, 7:42 PM

@Jeff, check @Temur's answer. The product_id field in this situation (which happens in javascript) will only provide you with the database ID and thus cannot be accessed using dot notation.

on 2/13/15, 4:18 AM

Got it working, thanks!

Jeff Beidler
on 2/13/15, 12:04 PM

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

1 follower(s)


Asked: 2/12/15, 4:04 PM
Seen: 1961 times
Last updated: 3/16/15, 8:10 AM