You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 10中如何在Search ORM中获取计算字段值?值异常求助

Hey, let's work through this issue step by step. There are a couple of critical problems in your code that are making balance_amount stay False or fail to update when you set store=True:

1. You're not assigning a value to balance_amount in your compute method

Looking at your code snippet, you calculate total_paid but never actually assign a result to balance_amount! That's why it's always False—Odoo has no value to store or return. You need to add the final calculation line based on your business logic. For example, if balance is the loan principal minus total paid:

@api.multi
@api.depends('loan_lines.paid', 'loan_lines.amount')
def _compute_loan_amount(self):
    for loan in self:
        total_paid = 0.0
        for line in loan.loan_lines:
            if line.paid:
                total_paid += line.amount
        # Replace with your actual balance calculation logic
        loan.balance_amount = loan.principal - total_paid

2. Incorrect decorator usage & missing dependencies for store=True

  • The @api.one decorator in Odoo 10 converts the recordset to a single record, so looping over self is redundant and error-prone. Switch to @api.multi paired with @api.depends instead—it's cleaner for handling multiple records.
  • When you set store=True, Odoo needs to know which fields trigger a recalculation. You must define these dependencies with the depends parameter on the field, otherwise it'll never update the stored value.

Update your field definition like this:

balance_amount = fields.Float(
    string="Balance Amount",
    compute='_compute_loan_amount',
    store=True,
    depends=['loan_lines.paid', 'loan_lines.amount', 'principal']  # Add all fields your calculation relies on
)

3. Making the field accessible to Search ORM

  • If you use store=True, the field value is saved to the database, so you can query it with Search ORM just like any regular field (e.g., self.env['your.loan.model'].search([('balance_amount', '>', 0)])).
  • If you don't want to store the field (skip store=True), you'll need to define a custom search method to handle queries, since Odoo can't search on non-stored computed fields directly. Here's a quick example (adjust to your logic):
balance_amount = fields.Float(
    string="Balance Amount",
    compute='_compute_loan_amount',
    search='_search_balance_amount'
)

def _search_balance_amount(self, operator, value):
    # Example logic: Translate balance query to principal vs total paid
    return [
        ('principal', operator, value + sum(
            self.env['loan.line'].search([('loan_id', '=', self.id), ('paid', True)]).mapped('amount')
        ))
    ]

That said, using store=True is the simpler, more reliable approach for searchability.

Quick Validation Steps

  1. After updating your code, upgrade your module and check if the balance_amount column exists in your loan model's database table.
  2. Edit a loan record, mark a line as paid, save it, and verify balance_amount updates correctly.
  3. Test your Search ORM query to confirm it returns the right records.

内容的提问来源于stack exchange,提问作者M Tahir Noor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:40:49