简体   繁体   English

Odoo,Many2One 字段域在编辑记录时不起作用

[英]Odoo, Many2One field domain not working when editing a record

In my odoo module I have a internal_division model and a cover model.在我的 odoo 模块中,我有一个 internal_division model 和一个封面 model。 Every internal divisiom has a cover (Many2One field) and a cover can be a forest ('is_forest' boolean field).每个内部分区都有一个封面(Many2One 字段),并且封面可以是一个森林('is_forest' boolean 字段)。 Internal division also has a with_forest_cover boolean field.内部划分还有一个 with_forest_cover boolean 字段。 Every time the 'with_forest_cover' changes to True, the cover_id field must change its domain to those having 'is_forest'=True, and the other way around.每次 'with_forest_cover' 更改为 True 时,cover_id 字段必须将其域更改为具有 'is_forest'=True 的域,反之亦然。 I was trying to do it using:我试图这样做:

cover_id = fields.Many2one('cusaf.cover', string='Cobertura',domain="[('is_forest', '=', with_forest_cover)]")

But for some reson this is only working when creating a new internal_division.但是对于某些原因,这仅在创建新的 internal_division 时才有效。 When I edit an existing internal division and select / deselect the 'with_forest_cover' check box, the cover_id field is not changing its domain.当我编辑现有的内部部门和 select / 取消选择“with_forest_cover”复选框时,cover_id 字段不会更改其域。 This is only happening when editing an existed internal division.仅在编辑现有的内部部门时才会发生这种情况。 Why is this happening.为什么会这样。 I am working with odoo v14 Here is my code我正在使用 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')

You won't be able to access to the with_forest_cover value inside cover_id domain, 'cause your class is instantiated before the client, in that time there aren't values.您将无法访问cover_id域内的with_forest_cover值,因为您的 class 在客户端之前实例化,当时没有值。 You have 1 easy, 1 intermediate and 1 elevated(not worth the effort) solutions:您有 1 个简单、1 个中级和 1 个提升(不值得努力)的解决方案:

Important: Delete domain for cover_id in.py重要:删除cover_id in.py 的域

Easy: Domain in XML.简单: XML 中的域。 Be sure with_forest_cover is declared in the XML.确保在with_forest_cover中声明了 with_forest_cover。

<field name="cover_id" domain="[('is_forest', '=', with_forest_cover)]"/>

Intermediate: take advantage of your with_forest_cover onchange to retrieve a domain for cover_id dynamically中级:利用您的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)]}
    }

I hope this could be helpful for you.我希望这对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM