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

Odoo 10技术求助:从分析账户取project_id及销售订单行关联配置

Answers to Your Odoo 10 Technical Questions

Hey there! Let's work through your two Odoo 10 questions step by step.

1. How to Get project_id from an Analytic Account?

In Odoo 10, the project.project model has a many2one field linking to analytic.account (named analytic_account_id). This means an analytic account can have one or more related projects (though most setups use a one-to-one relationship here).

To fetch the associated project_id from an analytic account record:

  • Use the reverse one2many relationship (Odoo automatically creates this as project_ids for the analytic.account model)
  • Grab the first item in the list (always handle cases where no project is linked to avoid errors)

Here's a code snippet example, assuming you have an analytic account record stored in analytic_account:

# Get the first associated project (if exists)
project = analytic_account.project_ids[:1]
project_id = project.id if project else False

If your setup enforces one project per analytic account, this will reliably return the correct project_id. If there's no linked project, it returns False—make sure to handle that case in your code if needed.

2. Add a Computed project_id Field to sale_order_line

To auto-populate a project_id field on sale.order.line based on the existing analytic_account_id, you'll need to add a computed field that depends on the analytic_account_id field. Here's how to implement it:

Step 1: Inherit the sale.order.line Model

Create a custom module (or add to an existing one) with this code:

from openerp import models, fields, api

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    project_id = fields.Many2one(
        'project.project',
        string='Project',
        compute='_compute_project_id',
        depends=['analytic_account_id'],
        store=True,  # Set to True if you want to store the value (enables searching/filtering)
        readonly=True
    )

    @api.multi
    @api.depends('analytic_account_id')
    def _compute_project_id(self):
        for line in self:
            if line.analytic_account_id:
                # Fetch the linked project from the analytic account
                linked_project = line.analytic_account_id.project_ids[:1]
                line.project_id = linked_project.id if linked_project else False
            else:
                line.project_id = False

Key Notes:

  • store=True: This makes Odoo save the computed value to the database. Use this if you need to search/filter by project_id on sale order lines. Odoo will automatically update the value whenever analytic_account_id changes.
  • If you don't need to store the value (just display it), remove store=True—the field will compute dynamically when the record is loaded.
  • The @api.depends('analytic_account_id') decorator tells Odoo to re-run the compute method whenever the analytic_account_id field changes.

Step 2: Update the View

To show the new project_id field on sale order line forms or lists, add this to your view XML:

<!-- Example: Add the field right after analytic_account_id -->
<field name="analytic_account_id"/>
<field name="project_id"/>

That's it! Now whenever you set an analytic_account_id on a sale order line, the project_id will auto-populate with the linked project (if one exists).


内容的提问来源于stack exchange,提问作者weluvu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:29:34