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

标签 python python-2.7 odoo-8 odoo

考虑这四种模型:

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 之间应该是相互关联的。

例如,如果 qtybsi.production.order.lines是 10,(还有其他方法可以从生产订单创建 bsi.print.order),并且在 bsi.print.order 上我穿上qty值5,原来的10应该是bsi.production.order.line上的5 ,我想用类似的方法,如 _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创建方法),在这里你发现了问题:

@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

这样,您的生产线和打印线就会相关,并且您必须覆盖 writeunlink方法也可以控制何时修改或删除行,对其“孪生”执行相同的操作(由于名为 Many2one 的新 po_line_related 字段,很容易找到它)。

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

关于python - 计算从一种模型到另一种模型的字段数量 - Odoo v8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47297950/

相关文章:

python - 在不知道数据大小的情况下从文件中读取 float 作为 numpy 数组

python - 如何修改 'date'变化的时间(00 :00:00) in an index in Pandas dataframe?

python - 当我使用PyCharm和odoo时,[Errno 2]没有这样的文件或目录,该过程以退出代码2结尾

python - 获取PCAP文件中的所有ip

python - 在 Windows 上为 Python 安装 Pillow

python - del self vs self.__del__() - 在 python 中清理的正确方法是什么?

python - odoo context.get.active_id 不工作

python-2.7 - 如何在文本字段中设置字符限制..?

python - Discord channel 成员(member)只返回一名成员(member)?

python - 如何使用 app.yaml 为 Google Appengine Python 创建自定义错误页面