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

for example 

x = fields.many2one(); 

and value of 'x' can be one of  'a', 'b' or 'c'; now how shall I set value of 'x' as 'b' by code?

อวตาร
ละทิ้ง

Hello please give some more details about what you are trying to do. Are you trying to give a default value for the many2one field.

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

Hi,

Review below links. they will helpful for you.

https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/

https://odoo-development.readthedocs.io/en/latest/dev/py/fields.html#many2one


อวตาร
ละทิ้ง
ผู้เขียน

thanks for your reply

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

Hi,

Many2one relation returns records of related model. Many2one field store record id of that related model. For reference check this link​   

You can use default method to load default value in many2one field 

field = fields.Many2one('related_model', string="", default=_default_value, required=True)
@api.model
def _default_value(self):
    return self.env['related_model'].search([], limit=1)
อวตาร
ละทิ้ง
ผู้เขียน

thank you

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

The value in "x" is the record that you have selected.
It stores the 'id' of that referencing record. So directly you cannot store the "value" to that, but by setting the id of the record will set the value. It is Integer value that you can set in "x".

อวตาร
ละทิ้ง
ผู้เขียน

thanks

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ก.พ. 25
5979
1
ธ.ค. 24
1506
1
พ.ย. 22
16081
3
ส.ค. 22
13144
2
ส.ค. 22
4579