Odoo - 过滤父 class 中的子记录

Odoo - Filter child records in parent class

我正在使用 Odoo 10-e。我正在研究一个示例模块。 我有 5 个模型(客户、产品、订单、订单详细信息、移动)

Customer :

class Customer(models.Model):
    _name ="amgl.customer"
    …… all other fields related to customer 
    product_ids = fields.Many2many('amgl.products', string='Products')

Products :

class Products(models.Model):
    _name ="amgl.products"
    …… all other fields related to product
    customer_ids = fields.Many2many('amgl.customer', string='Customers')

Order :

class Order(models.Model):
    _name ="amgl.order"
    order_line = fields.One2many('amgl.order_line', 'order_id', string='Order Lines')
    …… all other fields related to Order

Order Details :

class OrderLine(models.Model):
    _name = 'amgl.order_line'

    order_id = fields.Many2one('amgl.order', string='Orders')
    products = fields.Many2one('amgl.products', string="Products")
    …… all other fields related to order details

Movement

class Movement(models.Model):
    _name = 'amgl.metal_movement'

    customer = fields.Many2one("amgl.customer", string="Customer", required=True)
    order_lines = fields.One2many('amgl.order_line','metal_movement_id')

    …… all other fields related to movement

我想做的是,我正在创建一个移动表单,其中用户将 select 客户中的客户首先下拉,然后用户将添加 order_lines 并且他将是能够添加产品 我想过滤与上述 selected 客户相关联的产品。我怎样才能做到这一点 ?我从上个月就开始尝试了。

Movement View

<odoo>
<data>
    <record id="action_metal_movement" model="ir.actions.act_window">
        <field name="name">Metal Movement Request</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">metal.movement</field>
        <field name="view_mode">tree,form</field>
        <field name="help" type="html">
          <p class="oe_view_nocontent_create">
            Click to create
          </p><p>
            <!-- More details about what a user can do with this object will be OK -->
          </p>
        </field>
    </record>
    <record id="form_metal_movement" model="ir.ui.view">
        <field name="name">Metal Movement Request </field>
        <field name="model">metal.movement</field>
        <field name="arch" type="xml">
            <form string="Metal Movement">
                <sheet>
                    <group>
                    <group string="Metal Movement Request">
                        <field name="date_create" string="Date Created"/>
                        <field name="reference"/>
                        <field name="metal_movement_type"/>
                        <field name="first_approve"/>
                        <field name="second_approve" domain="[('id', '!=', first_approve)]"/>
                        <field name="customer"/>
                        <field name="sepcial_instruction" widget="html"/>
                    </group>
                    <group string="Metal Movement From">
                        <group colspan="6">
                            <field name="custodian"/>
                            <field name="mmf_name"/>
                            <field name="mmf_account_number"/>
                            <field name="mmf_account_type"/>
                        </group>
                        <group string="Metal Movement To" colspan="6">
                            <field name="mmt_name"/>
                            <field name="mmt_address" attrs="{'invisible':[('metal_movement_type', '=', 'IT')]}"/>
                            <field name="mmt_account_number" />
                            <field name="mmt_company" attrs="{'invisible':[('metal_movement_type','not in',('AC','IPPU','IT'))]}"/>
                            <field name="pickup_date" string="Pick up Datetime" attrs="{'invisible':[('metal_movement_type','not in',('AC','IPPU'))]}"/>
                        </group>
                    </group>
                    <group string="Metals To Be Moved"  colspan="12">
                        <field name="order_lines">
                            <tree editable="bottom">
                                <field name="quantity" string="Quantity"/>
                                <field name="products" domain="[('customer_ids','in', parent.customer)]" string="Product Name"/>
                                <field name="weight" string="Weight"/>
                                <field name="total_weight" string="Total Weight"/>
                            </tree>
                        </field>
                    </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>
    </data>
    </odoo>

感谢您更新问题。

首先在产品字段上下文中添加客户。

<field name="order_lines">
    <tree editable="bottom">
        <field name="quantity" string="Quantity"/>
        <field name="products" context="{'customer_id':parent.customer}" string="Product Name"/>
        <field name="weight" string="Weight"/>
        <field name="total_weight" string="Total Weight"/>
    </tree>
</field>

然后在产品模型中这样写name_get方法

@api.multi
def name_get(self):
    if self.env.context('customer_id',False):
        customer = self.env['amgl.customer'].browse(self.env.context('customer_id',False))
        for product in customer.product_ids:
            res.append((product.id,product.name)) 
    else:
         res=super(product_product,self).name_get()
    return res

就是这样。