Skip to main content
 首页 » 编程设计

python之计算从一个模型到另一个模型的字段数量之Odoo v8

2024年10月01日6jiqing9006

考虑这四种模型:

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) 

因此,生产订单有生产订单行,打印订单也有它的订单行(One2many order_lines 字段)

两者都有一个方法,都叫做_remaining_func_。

这些对于 remaining_qty 字段是可以的,但是 consumed_qty 应该在 production.order 和 print.order 之间相互关联。

因此,例如,如果 bsi.production.order.lines 上的 qty 是 10,(还有其他方法可以创建 bsi.print。 order 来自生产订单),在 bsi.print.order 上我把 qty 的值设为 5,原来的 10 在 bsi 上应该是 5 .production.order.line,我认为使用类似_remaining_func_ 的方法我可以做到这一点,但我对如何在两个模型之间做到这一点感到困惑。

有什么想法吗?

如果需要进一步解释,请告诉我。

请您参考如下方法:

除非bsi.production.order之间的关系,否则您想要的是不可能管理的和 bsi.print.order是 1:1,但在您的情况下,生产订单似乎可以有很多打印订单。我举个例子:

您可以创建一个 Many2one bsi.print.order.line 中的字段指向 bsi.production.order.line :

class bsi_print_order_lines(models.Model): 
    _name = 'bsi.print.order.lines' 
 
    po_line_related = fields.Many2one('bsi.production.order.lines', ondelete='cascade', string="Production Order Line Related") 

并且每次创建打印线时,您都可以轻松创建相关的生产线(您拥有所需的所有数据):

@api.model 
def create(self, vals): 
    print_line = super(bsi_print_order_lines, self).create(vals) 
    po_line_vals = { 
        'production_order': print_line.print_order.production_orders.id, 
        'isbn': print_line.isbn, 
        'qty': print_line.qty, 
        'consumed_qty': print_line.consumed_qty, 
        'remaining_qty': print_line.remaining_qty, 
    } 
    po_line = self.env['bsi.production.order.lines'].create(po_line_vals) 
    return print_line 

但是你必须反过来做同样的事情(这次覆盖 bsi.production.order.lines ORM create 方法),这里你会发现问题:

@api.model 
def create(self, vals): 
    po_line = super(bsi_production_order_lines, self).create(vals) 
    print_line_vals = { 
        'production_orders': po_line.production_order.id, 
        'po_line_related': po_line.id, 
        'isbn': po_line.isbn, 
        'qty': po_line.qty, 
        'consumed_qty': po_line.consumed_qty, 
        'remaining_qty': po_line.remaining_qty, 
        'print_order': '???????'  # You cannot know which print order you have to write here since a production order can have several ones... 
    } 
    print_line = self.env['bsi.print.order.lines'].create(print_line_vals) 
    return po_line 

如果bsi.production.order之间的关系和 bsi.print.order是 1:1,你可以用 search 得到打印顺序(因为您可以确定只会返回一条记录):

@api.model 
def create(self, vals): 
    po_line = super(bsi_production_order_lines, self).create(vals) 
    print_order = self.env['bsi.print.order'].search([ 
        ('production_orders', '=', po_line.production_order.id) 
    ]).ensure_one() 
    print_line_vals = { 
        'production_orders': po_line.production_order.id, 
        'po_line_related': po_line.id, 
        'isbn': po_line.isbn, 
        'qty': po_line.qty, 
        'consumed_qty': po_line.consumed_qty, 
        'remaining_qty': po_line.remaining_qty, 
        'print_order': print_order.id, 
    } 
    print_line = self.env['bsi.print.order.lines'].create(print_line_vals) 
    return po_line 

这样你的生产线和打印线就会相关联,你将不得不覆盖 write和 unlink控制一行何时被修改或删除的方法,对它的“双胞胎”做同样的事情(由于新的 Many2one 字段名为 po_line_related ,很容易找到它)。

当然这不是一个漂亮的解决方案,但我认为它是您的实体关系图的唯一解决方案(使用 Odoo API)。