Skip to Content
Menu
This question has been flagged
1 Reply
2114 Views

Hi folks;

I was wondering to get some help here. I have a field called "container_id" which have an attribute"transport" on Sale.Order form. Basically this show me what kind of transport have that container (container_id.transport). This works good but the tricky part goes when i want to find that attribute from Sale.Order.Line on a _onchange method to validate what kind of transport is.  

What im trying is, basically, when im doing the math calculation to see the Volume (exp_vol) before i want to check if the container_id.transport is air or not because the formula changes. 

Attached i send you my code and screen captures. Thanks so much for your help. I am pretty sure i am not the only one trying to get some field from the top to Sale.Order.Line. Thanks in advance


XML

<record id="view_freight_operation_form" model="ir.ui.view">

<field name="name">view.freight.operation.form</field>

<field name="model">freight.operation</field>

<field name="arch" type="xml">

<form string="Freight">

  <sheet>

    <label for="name" string="Name"/>

    <field name="container_id"/>

    <notebook>

      <page string="Packaging">

        <field name="operation_line_ids" attrs="{'readonly': [('state','not in',['draft'])]}">

          <tree editable="top">

            <field name="length" string="L (in)"/>

            <field name="width" string="W (in)"/>

            <field name="height" string="H (in)"/>

            <field name="exp_vol" sum="Total Volume" readonly="1" force_save="1"/>

          </tree>

        </field>

      </page>

    </notebook>

  </sheet>

</form>

</field>

</record>


PY

class FreightOperation(models.Model):

    """Freight Operation Model"""

    _name = 'freight.operation'

    _description = 'Freight Operations'


container_id = fields.Many2one('freight.container', string="Container")

    

class FreightOperationLine(models.Model):

    """Freight Operation Line Model"""

    _name = 'freight.operation.line'

    _description = 'Order Line'

    

container_id = fields.Many2one('freight.container', string="Container")

length = fields.Float(string='Length')

width = fields.Float(string='Width')

height = fields.Float(string='Height')

exp_vol = fields.Float(string="Volume")


@api.onchange('length', 'width', 'height')

    def _onchange_length(self):

        for operation in self:

            if operation.container_id.transport == 'air':

                for line in self:

                    line.exp_vol = line.length * line.width * line.height / 166

            else:

                for line in self:

                    line.exp_vol = line.length * line.width * line.height / 1728

Avatar
Discard
Best Answer

Please try with this example:

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

is_test = fields.Boolean(string="Is expired")

   <page string="Order Lines" name="order_lines">
                            <field name="is_test"/>
                            <field
                                name="order_line"
                                widget="section_and_note_one2many"
                                mode="tree,kanban"
                                attrs="{'readonly': [('state', 'in', ('done','cancel'))]}"
                                context="{'test_id':parent.is_test}">
                            >

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

    @api.onchange('product_id')
    def onchange_is_test(self):
        print("...........self..........",self._context)
        if self._context.get('test_id'):
            //your calculations
        else:
            //your calculations

Regards,




Email:      odoo@aktivsoftware.com  

Skype: kalpeshmaheshwari

   

Avatar
Discard