Search code examples
odoo-8odooodoo-10odoo-9odoo-view

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, i am creating a movement form in which user will select a customer from customer drop down first then user will add order_lines and in that he will be able to add products i want to filter products which are associated with above selected customer . How can i do that ? I have 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>

Solution

  • Thank you for updation of question.

    First add customer in product field context.

    <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 product's model write it's 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
    

    Thats it.