[Python] Compute fields quantities from one model to another - Odoo v8

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    Consider these four models:

    class bsi_production_order(models.Model):
    _name = 'bsi.production.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Production Date")
    production_type = fields.Selection([
    ('budgeted','Budgeted'),
    ('nonbudgeted','Non Budgeted'),
    ('direct','Direct Order'),
    ], string='Type of Order', index=True,
    track_visibility='onchange', copy=False,
    help=" ")
    notes = fields.Text(string="Notes")
    order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
    print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")

    class bsi_production_order_lines(models.Model):
    _name = 'bsi.production.order.lines'

    production_order = fields.Many2one('bsi.production.order', string="Production Orders")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    consumed_qty = fields.Float(string="Consumed quantity")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")

    @api.onchange('qty', 'consumed_qty')
    def _remaining_func(self):
    if self.qty or self.consumed_qty:
    self.remaining_qty = self.qty +(-self.consumed_qty)

    class bsi_print_order(models.Model):
    _name = 'bsi.print.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Print Date")
    origin = fields.Char(string="Origin")
    production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
    order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")

    class bsi_print_order_lines(models.Model):
    _name = 'bsi.print.order.lines'

    print_order = fields.Many2one('bsi.print.order', string="Print Order")
    production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Integer(string="Quantity")
    consumed_qty = fields.Integer(string="Quantity consumed")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")

    @api.onchange('qty', 'consumed_qty')
    def _remaining_func(self):
    if self.consumed_qty or self.qty:
    self.remaining_qty = self.qty +(-self.consumed_qty)


    So, production order has production order lines, and print order has it's order lines too (One2many order_lines fields)

    There is a method on both of them, which are both called _remaining_func_.

    These are OK for remaining_qty fields, but consumed_qty should be inter-related between production.order and print.order.

    So, for example, if qty on bsi.production.order.lines is 10, (There are other methods which create a bsi.print.order from production order), and on bsi.print.order I put on qty the value 5, the original 10 should be 5 on bsi.production.order.line, I think with a similar method like _remaining_func_ I can achieve this, but how can I do this between the two models?

    Continue reading...

Compartilhe esta Página