Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
6703 มุมมอง

Is it possible to have writable related field ? I tried by adding a 'reverse' parameter in my field definition and having the reverse method writing the value in related field. However, in the view, the field is always readonly. Here is my code:

 gift_instructions = fields.Char(
        related='invoice_line_id.gift_instructions', store=True,
        inverse='_set_gift_instructions')
@api.one def _set_gift_instructions(self): self.invoice_line_id.gift_instructions = self.gift_instructions

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

How about a computed field instead ?

You already have your setter function, you just need to create your getter function:

@api.depends('invoice_line_id')
def _get_gift_instructions(self):
self.gift_instructions = self.invoice_line_id.gift_instructions

and link it:

gift_instructions = fields.Char(
        compute='_get_gift_instructions', store=True,
        inverse='_set_gift_instructions'
อวตาร
ละทิ้ง
ผู้เขียน

Thanks, it works ! I thought related field would behave like computed fields, but apparently they are not writable.

คำตอบที่ดีที่สุด

For a related field, when we change the field at form view, it automatically changes the relational model too
i.e. No need to use reverse to write at gift_instructions and it will be automatically done by the Object-relational mapping(ORM).

Check whether you have set the gift_instructions field as readonly at XML form view.

For more info check the odoo documentation here.

อวตาร
ละทิ้ง

I didn't knew that. In fact, I never used related fields, but that could change for sure, thanks :)

Related Posts ตอบกลับ มุมมอง กิจกรรม
7
ก.ย. 20
7886
1
พ.ย. 17
4449
0
พ.ย. 16
4298
1
ม.ค. 24
33240
Notes module - I see everyone's notes แก้ไขแล้ว
3
ธ.ค. 15
7194