Odoo - Filter child records in parent class

I am using Odoo 10-e. I am working on a sample module. I have 5 models (customer, product, order, order details, movement)

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

      

What I am trying to do is create a movement form where the user will first select a customer from the customer and then add order_lines

, and in this he will be able to add products that I want to filter the products that are related to the selected customer. How can i do this? I've been trying since last month.

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>

      

+3


source to share


1 answer


Thanks for updating the question.

First add the customer to the context of the product field.

<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>

      



Then, in the product model, write its name_get method like this.

@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

      

Here it is.

+2


source







All Articles