Odoo11为已有模块新增字段失败,请求排查原因
Hey there, let's walk through why your custom field isn't appearing in the Product Template form. Based on the code snippet you shared, here are the most common fixes to check:
First, make sure you defined the field in the Python model
It's easy to forget this critical step! You need to inherit theproduct.templatemodel in your custom module's Python file and add the field explicitly:from odoo import models, fields class ProductTemplate(models.Model): _inherit = 'product.template' # Replace with your actual field definition custom_field_name = fields.Char(string="My Custom Field")Without this, the view has no data source to pull from, so the field will never show up.
Fix the extra space in your inherit_id reference
In your XML, you have a trailing space in the ref attribute:ref="product.product_template_only_form_view "— Odoo's XML IDs are case and whitespace-sensitive. Remove that space to correctly reference the parent view:<field name="inherit_id" ref="product.product_template_only_form_view" />A wrong reference means your view inheritance doesn't kick in at all.
Verify your XPath expression is complete and correct
Your snippet cuts off at<xpath expr="//field[@n...— you need a full, valid XPath to target where you want to insert the field. For example, to add your field right after the "Sale Price" field (list_price), use:<xpath expr="//field[@name='list_price']" position="after"> <field name="custom_field_name" /> </xpath>If your XPath doesn't match any elements in the parent view, the field won't be inserted. Use your browser's dev tools (F12) to inspect the original form's structure and confirm the correct target.
Ensure your module is properly updated and has dependencies set
After making changes, go to Odoo's Apps menu, find your custom module, and click Update. Also, double-check your__manifest__.pyfile to confirm it listsproductas a dependency:'depends': ['product'],This ensures Odoo loads your module after the core Product module, so inheritance works as expected.
Check permissions for the field
Even if the field exists, the current user might not have permission to see it. Either:- Add read access to the relevant groups in your module's
security/ir.model.access.csv - Set the
groupsparameter in your field definition (e.g.,groups="base.group_user") - Add
readonly="0"to the field in the view if you want users to edit it
- Add read access to the relevant groups in your module's
Check Odoo logs for errors
If all else fails, look at Odoo's log file. It will show specific errors like XML parsing issues, missing field references, or inheritance failures — these clues will point you straight to the problem.
内容的提问来源于stack exchange,提问作者khaleesi

