Odoo python中的Many2one和One2Many错误

问题描述 投票:0回答:1

我在用sale.order继承了一个类而另一个类只是一个__name或与其关联的地方遇到了这个问题。

class module_A(models.Model):
       _name='new.module_a'
      
       sale_id = fields.One2many(comodel_name='sale.order')
       currency_id = fields.Many2one('res.currency',     string="currency")
       price_value = fields.Monetary(related="sale_id.total_price", string="Initial Price value",
                                          currency_field="currency_id")

class module_B(models.Model):
       
    _inherit = 'sale.order'
    module_id = fields.One2many(string="module A",
                                    comodel_name='new.module_a', inverse_name='sale_id')
    total_price = fields.Monetary(string="Price Initial", store=True, readonly=True, compute='_amount_all',
                                  tracking=4)
    amount_untaxed = fields.Monetary(string="Untaxed Amount", store=True, readonly=True, compute='_amount_all',
                                     tracking=5)
    amount_tax = fields.Monetary(string="Taxes", store=True, readonly=True, compute='_amount_all')

    @api.depends('order_line.price_total')
    def _amount_all(self):
        for order in self:
            amount_untaxed = amount_tax = 0.0
            for line in order.order_line:
                amount_untaxed += line.price_subtotal
                amount_tax += line.price_tax

            total_price = amount_tax + amount_untaxed
            print(total_price)
            order.update({
                'amount_untaxed': amount_untaxed,
                'amount_tax': amount_tax,
                'amount_total': amount_untaxed + amount_tax,
                'total_price': amount_untaxed + amount_tax,
            })

问题:1.在树形视图中,price_value为0。这意味着在与sale_id相关联后,没有从total_price中获取值。我不知道为什么

但是当我用tree_view分隔它时。价格显示。enter image description here

xml:

<field name="module_id"/>
         tree view
             control
             field name="price_value" <- field in module A

    <group>
        <field name="total_price"/> <- field in module B

注意:这些字段不能互换,因为total_price将查看继承的sale.order,而module_b中不存在price_value。

  1. 如果我总计所有值,我还需要来自类module_A的值并将其放在module_B中。这是一个大问题,因为它们将使用相同的列和表,因此将无法使用。

  2. 我必须在树形视图的每个项目中添加一个小计,我必须在其中考虑数字2的问题。示例在类module_A中计算总值,并从A到B获取值,以便可以在视图中显示它。

python one-to-many multiple-inheritance many-to-one odoo-13
1个回答
0
投票

在型号_name='new.module_a'上,将sale_id字段one2many设置为many2one,

sale_id = fields.Many2one('sale.order', string="Sale")

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.