【问题标题】:Many2one and One2Many Errors in Odoo pythonOdoo python中的Many2one和One2Many错误
【发布时间】:2020-04-03 03:35:32
【问题描述】:

我遇到了这个问题,我用 sale.order 继承了一个类,而另一个类只是一个 __name 或与之关联。

class module_A(models.Model):
       _name='new.module_a'
      
       sale_id = fields.Many2one(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 分开时。显示价格。

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 的值,以便我可以在视图中显示它。

初始价格值仍然为零。

【问题讨论】:

  • 注意:module_B中的计算如图所示为141k,但tree中的初始价格仍为0。

标签: python one-to-many multiple-inheritance many-to-one odoo-13


【解决方案1】:

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

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

谢谢

【讨论】:

  • 不起作用。如果我选择那个,我无法创建报价。 "对不起,你不能访问'new.module_a'(new.module_a)类型的文档。目前没有组允许这个操作
  • @William 检查此模型的访问权限 - new.module_a。看来您还没有将它们添加到安全文件中。
  • 我已经更改了安全性。仍然不起作用。我编辑了从 One2Many 到 Many2one 的所有内容。初始价格仍然为零。
  • 根本不起作用。初始价格仍然为零。我已经把它放在一个只读字段上。
【解决方案2】:

我已经找到了这个问题的答案。只是为了让architect_ids依赖于api.depends来访问模块A中的字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多