Hello everyone,
I am using Odoo 12 and need to check for a condition that is based on a child field from a "many2one" relation.
What I have:
class Model1(models.Model):
_name = 'model1'
_description = 'This is the model1'
code = fields.Char('Model 1 Code', required=True)
name = fields.Char('Model 1 Name', required=True)
reference_required = fields.Boolean('Requires a Reference', default=False)
class Model2(models.Model):
_name = 'model2'
_description = 'This is the model2'
#some fields here
relation_to_model1_id = fields.Many2one('model1',)
What I am trying to do, is to check if the "reference_required" field on "model1" is true and if so, "relation_to_model1_id" field is shown.
Tried with no luck:
<field name="relation_to_model1_id" attrs="{'invisible': [('relation_to_model1_id.reference_required','=',False)]}"/>
How can I achieve this on a form view?
I cannot even add a child field to the "model2" form view, using for instance:
<field name="relation_to_model1_id.code"
Can anyone help achieve this?
Thank you in advance
PM
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Kế toán
- Tồn kho
- PoS
- Project
- MRP
Câu hỏi này đã bị gắn cờ
Have you tried adding a related field in Model2, i.e.
model1_id_reference_required = fields.Boolean(related='relation_to_model1_id.reference_required')
Then you can add it to the view and use it domain filters.
For reference https://www.odoo.com/documentation/13.0/reference/orm.html#reference-fields-related
Dear @Alessandro Fiorino,
Thank you very much (and all others also).
You were right. The method you described solved my problem.
I have tried later but having an inconsistency error because I was using the wrong field type on the destination related field and the origin field.
Problem solved.
Best regards
PM
Hi Paulo:
Something does not seem right with the example. You are self-referencing in the attrs attribute of relation_to_model1_id. The value of relation_to_model1_id.reference_required cannot be determined till the value of relation_to_model1_id is set.
Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!
Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!
Đăng kýBài viết liên quan | Trả lời | Lượt xem | Hoạt động | |
---|---|---|---|---|
|
1
thg 4 19
|
9937 | ||
|
1
thg 3 22
|
11601 | ||
|
1
thg 9 21
|
5605 | ||
|
5
thg 9 19
|
6246 | ||
|
0
thg 3 20
|
3374 |
Odoo Technical Tips: https://learnopenerp.tumblr.com/