采购订单行 Odoo 10 中的域过滤器

Domain filter in purchase order line Odoo 10

我需要根据父级 table (purchase.order) 中的值过滤采购订单行中的产品。

Purchase_order.py

class PurchaseOrder(models.Model):
   _inherit = 'purchase.order'
   product_variety_type = fields.Selection([('raw_material','Raw Material'),('stationary','Stationary')],string='Product Variety')

我已经在 purchase.order.line.py 中定义了域过滤器,需要再添加一个条件 ('variety','=',self._context.get('variety')

 class PurchaseOrderLines(models.Model):
_inherit = 'purchase.order.line'


@api.onchange('product_id')
@api.depends('order_id')
def onchange_product_id2(self):

    product_variety_type = self._context.get('variety') // Value is None
    result = super(PurchaseOrderLines,self).onchange_product_id()
    supplier_infos = self.env['product.supplierinfo'].search([('name', '=', self.partner_id.id)])
    product_ids = self.env['product.product']
    if not supplier_infos:
        product_ids = product_ids.search([])
    for supplier_info in supplier_infos:
         product_ids += supplier_info.product_tmpl_id.product_variant_ids

    result.update({'domain': {'product_id': [('id', 'in', product_ids.ids)]}})

    return result

我试图从父视图传递上下文值,但没有成功。

*.xml

  <field name="partner_id" position="after">
        <field name="product_variety_type" required="1" context="
        {'variety':product_variety_type}"/>

  </field>

我该怎么做?

您不必使用 context。只需使用 self.order_id 即可获取值。

@api.onchange('product_id')
def onchange_product_id2(self):
    product_variety_type = self.order_id.product_variety_type
    # and so on