【问题标题】:How to fix the dynamic domains issue in Odoo?如何解决 Odoo 中的动态域问题?
【发布时间】:2017-11-10 11:05:02
【问题描述】:

我创建了一个名为product.service.type 的新模型。然后,在product.product模型中,我还创建了一个Many2many字段(命名为service_type,指向product.service.type模型)。

现在我有了模型test,它有product_idservice_type_id 字段,Many2one 分别指向product.productproduct.service.type

我想要的是,如果您选择产品,服务类型域会更改为仅显示所选产品的服务类型。我通过onchange 管理了这个:

def onchange_product_id(self, cr, uid, ids, product_id, context=None):
    if product_id:
        product = self.pool.get('product.product').browse(
            cr, uid, [product_id], context=context)
        service_type_ids = product.service_type.mapped('id')
        return {
            'domain': {
                'service_type_id': [('id', 'in', service_type_ids)],
            },
        }

这很好用,问题出在您正在编辑记录(而不是创建新记录)时,因为在这种情况下,onchange 没有被执行,因此域显示所有服务类型。

您可以在合作伙伴表单中看到相同的问题,字段为title。创建一个新的合作伙伴,它是一家公司,title 字段的域更改为只允许您选择像 Corp.Ltd. 等记录,但如果您设置合作伙伴为联系人,您可以在Doctor女士Miss等记录中进行选择。现在,将合作伙伴与您想要的数据,然后转到顶部栏的其他菜单。返回合作伙伴表单并打开创建的合作伙伴进行编辑。检查title 字段而不更改is_company 字段。现在,尽管您的合作伙伴属于特定类别(公司或联系人),但您拥有所有可用的头衔。

我该如何解决这个问题?

【问题讨论】:

    标签: python python-2.7 odoo-8 odoo


    【解决方案1】:

    我认为,有以下几点。是可以实现的。

    • 在 xml 端的 many2many 字段中使用 product_id 更新上下文。
    • 覆盖产品服务类型模型的search()方法
    • 检查是否收到相同的上下文并触发逻辑,否则返回 super() 方法

    例如:

    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        context = self._context or {}
    
        # Display product list which has not included in CofA Template
        if context.get('product_service_id'):
            product = self.env['product.product'].browse(context.get('product_service_id'))
            service_type_ids = product.service_type.mapped('id')
            args += [('service_type_id', 'not in', service_type_ids)]
    
        return super(ProductServiceType, self).search(args,
                                                   offset,
                                                   limit,
                                                   order,
                                                   count=count)
    

    在 XML 端:

    <field name="product_id"/>
    <field name="many2many_field" context="{'product_service_id': product_id}">
    

    注意:我已尝试按照新 API 进行回答,但尚未对其进行测试。您需要使用旧 API 或根据您的要求对其进行转换。

    【讨论】:

    • 感谢您的回答,但不幸的是它不起作用。您告诉我将 XML 上下文添加到 many2many 字段,但该字段存在于 product.product(名为 service_type)和 product.service.type(名为 product_ids)中。您不是要将该上下文添加到 test 模型中名为 service_type_id 的 Many2one 字段吗?我也这样做了,但search 甚至没有被执行。
    【解决方案2】:

    在您的模型 test 上,除了您的 2 个字段之外,您还需要一个与对象 product.product 的字段 service_type 相关的额外 many2many。然后将域分配给您的字段service_type_id

    这是插图,

    product.product 型号上,您有:

    1. service_type(平方米到product.service.type

    test 型号上,您有:

    1. product_id(平方米到product.product
    2. service_type_id (m2o 到 product.service.type)
    3. available_service_type_ids(m2m 相关/计算到service_type of product.product
    4. 在您的 xml 中,service_type_id 将具有域 [(id , in, available_service_type_ids and available_service_type_ids[0] and available_service_type_ids[0][2] or False)]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      相关资源
      最近更新 更多