[英]Odoo, Many2One field domain not working when editing a record
在我的 odoo 模塊中,我有一個 internal_division model 和一個封面 model。 每個內部分區都有一個封面(Many2One 字段),並且封面可以是一個森林('is_forest' boolean 字段)。 內部划分還有一個 with_forest_cover boolean 字段。 每次 'with_forest_cover' 更改為 True 時,cover_id 字段必須將其域更改為具有 'is_forest'=True 的域,反之亦然。 我試圖這樣做:
cover_id = fields.Many2one('cusaf.cover', string='Cobertura',domain="[('is_forest', '=', with_forest_cover)]")
但是對於某些原因,這僅在創建新的 internal_division 時才有效。 當我編輯現有的內部部門和 select / 取消選擇“with_forest_cover”復選框時,cover_id 字段不會更改其域。 僅在編輯現有的內部部門時才會發生這種情況。 為什么會這樣。 我正在使用 odoo v14 這是我的代碼
class InternalDivision(models.Model):
_name = 'cusaf.internal_division'
_description = 'División interna'
_inherit = ['mail.thread', 'mail.activity.mixin']
name = fields.Char(string='Identificador externo', required=True)
_sql_constraints = [
('name_uniq', 'unique (name)',
"Error: Ya existe una división interna con el mismo identificador externo") ]
with_forest_cover = fields.Boolean(string='Área con cobertura de bosques naturales', default=False)
cover_id = fields.Many2one('cusaf.cover', string='Cobertura',domain="[('is_forest', '=', with_forest_cover)]")
cover_other = fields.Char(string='Otra cobertura')
cover_name = fields.Char(string='', related="cover_id.name")
@api.onchange('with_forest_cover')
def _with_forest_cover_onchange(self):
self.cover_id=None
class Cover(models.Model):
_name = 'cusaf.cover'
_description = 'Tipo de cobertura'
name = fields.Char(string='Nombre', required=True)
_sql_constraints = [
('name_uniq', 'unique (name)',
"Error: Ya existe un tipo de cobertura con el mismo nombre")
]
description = fields.Text(string='Descripción')
is_forest = fields.Boolean(string='Es bosque')
您將無法訪問cover_id
域內的with_forest_cover
值,因為您的 class 在客戶端之前實例化,當時沒有值。 您有 1 個簡單、1 個中級和 1 個提升(不值得努力)的解決方案:
重要:刪除cover_id
in.py 的域
簡單: XML 中的域。 確保在with_forest_cover
中聲明了 with_forest_cover。
<field name="cover_id" domain="[('is_forest', '=', with_forest_cover)]"/>
中級:利用您的with_forest_cover
onchange 動態檢索cover_id
的域
@api.onchange('with_forest_cover')
def _with_forest_cover_onchange(self):
return {
'value': {'cover_id': False},
'domain': {'cover_id': [('is_forest', '=', self.with_forest_cover)]}
}
我希望這對你有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.