Odoo 10技术求助:从分析账户取project_id及销售订单行关联配置
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
one2manyrelationship (Odoo automatically creates this asproject_idsfor theanalytic.accountmodel) - 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 byproject_idon sale order lines. Odoo will automatically update the value wheneveranalytic_account_idchanges.- 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 theanalytic_account_idfield 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

