odoo/openERP one2many字段动态域

yop*_*ngi 2 python openerp openerp-8

我想搜索依赖于另一个字段的one2many字段的记录.
这是我的代码,

家长班:

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    customer_product_ids = fields.One2many('product.product',
                           compute='_get_partner_products')
    order_line = fields.One2many('sale.order.line', 'order_id')
Run Code Online (Sandbox Code Playgroud)

儿童班:

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    order_id = fields.Many2one('sale.order')
    product_id = fields.Many2one('product.product')
Run Code Online (Sandbox Code Playgroud)

视图

 <record id="view_ata_sale_order_form_inherit" model="ir.ui.view">
        <field name="name">view.ata.sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <field name="partner_id" position="after">
                <field name="partner_channel_id" invisible="1"/>
                <field name="customer_product_ids"/>
            </field>
            <xpath expr="//tree/field[@name='product_id']" position="attributes">
                <attribute name="domain">[('id', 'in', [rec.id for rec in parent.customer_product_ids])]</attribute>
            </xpath>
        </field>
    </record>
Run Code Online (Sandbox Code Playgroud)

默认情况下,客户可以查看定义的所有产品.
在我的情况下,我需要过滤产品取决于客户选择.
每个客户可以拥有不同的列表产品,或者如果他们没有分类,他们可以看到所有产品.

我尝试了上面的代码,但得到了错误:

Uncaught Error: Expected "]", got "(name)"
Run Code Online (Sandbox Code Playgroud)

我想错误来自这段代码:

<attribute name="domain">[('id', 'in', [rec.id for rec in parent.partner_product_ids])]</attribute>
Run Code Online (Sandbox Code Playgroud)

我的问题,
是否有可能像上面的代码(在视图中)做python循环理解?

谢谢.

Mos*_*med 5

在您的情况下,您需要使用onchange动态域并将其返回到one2many字段

你应该有这样的事情:

@api.onchange('partner')
def onchange_partner(self):
    ids=search for products that have this partner
    then return some thing like this {'domain': {'Many2one or One2many': [('id', 'in', ids)}}
Run Code Online (Sandbox Code Playgroud)

我不会为你编写代码,所以你必须搜索onchange并应用它.你可以在这里找到一些信息.