如何为One2Many字段下的特定字段设置只读并解决QWeb2报错?
Hey there, let's break down why you're hitting this error and how to fix it. The error unknown field od_confirm_state_line happens because either your domain is referencing a field that doesn't exist in the right model, or you're using the wrong syntax for the domain in your One2Many tree view.
First, Let's Diagnose the Root Causes
- Field Doesn't Exist in the Child Model: The field
od_confirm_state_lineneeds to be present on the model that yourcost_lineOne2Many field points to (e.g., ifcost_linelinks toaccount.landed.cost.line, check if that model actually hasod_confirm_state_linedefined). Double-check for typos—underscores, capitalization, or missing suffixes/prefixes are common culprits. - Incorrect Domain Syntax: Odoo expects domain conditions to be a list of tuples, not a list of lists like you wrote (
[["od_confirm_state_line","=","confirmed"]]). Also, when setting readonly on a field inside the One2Many's tree view, you're working in the context of the child record, so you can directly reference the child model's fields without extra prefixes (unless you need to pull a value from the parent model—then useparent.).
Corrected Code Example
Let's assume you want to set a specific field (say product_id) to readonly when od_confirm_state_line is confirmed on the child record. Here's how your XML should look:
<page string="Landed Cost"> <field name="cost_line"> <tree editable="bottom"> <!-- Target field with corrected readonly domain --> <field name="product_id" readonly="[('od_confirm_state_line', '=', 'confirmed')]"/> <!-- Optional: Include the state field in the tree for testing visibility --> <field name="od_confirm_state_line"/> <!-- Add your remaining fields here --> </tree> </field> </page>
If you need to base the readonly state on a field from the parent model (not the child), adjust the domain to use the parent. prefix:
<field name="product_id" readonly="[('parent.od_parent_confirm_state', '=', 'confirmed')]"/>
Quick Verification Checklist
- Confirm
od_confirm_state_lineexists on the child model linked bycost_line - Fix the domain syntax to use tuples instead of nested lists
- If referencing parent model fields, add the
parent.prefix to the field name
That should resolve the QWeb2 runtime error and get your readonly logic working as expected.
内容的提问来源于stack exchange,提问作者vbt

