The error message KeyError: 'doc' in Odoo when validating a delivery slip indicates that the system is trying to access a variable or key named 'doc' that doesn't exist in the template or code being executed. This often occurs during the report generation process.
Here are steps to troubleshoot and resolve this problem:
1. Identify the specific report template:
- The traceback indicates the error is within a template file <198>. You'll need to find the exact name of the delivery slip template being used.
- In Odoo, go to Settings > Technical > Reporting > Reports. Search for "Delivery Slip" or a similar name related to delivery operations.
- Once you find the report, examine its External ID (e.g., stock.report_delivery_slip). This ID helps identify the associated QWeb template.
- Then, navigate to Settings > Technical > User Interface > Views and search for the external ID of the report to find the corresponding view.
2. Examine the QWeb template:
- Open the QWeb view identified in the previous step.
- Carefully review the template code, looking for where the variable doc is used. The error KeyError: 'doc' means that the code is trying to access a dictionary key or object property named doc, but it's not available in the context.
- Possible causes:
- Typo: A simple typo in the template code when referencing doc.
- Missing Definition: The variable doc is not being properly passed or defined within the template's context.
- Incorrect Context: The template is expecting a different type of object than what's being provided.
3. Check the report's Python code:
- The QWeb template receives its data from a Python class. You need to find the Python code that generates the data for the delivery slip report.
- Go to Settings > Technical > Actions > Reports. Find the delivery slip report. Check the "Model" field. This tells you which Odoo model is used as the basis for the report (e.g., stock.picking).
- Examine the code related to that model, especially any methods that are called when generating the delivery slip. Look for how the doc variable (or its equivalent) is being prepared and passed to the QWeb template.
- Possible issues:
- The Python code is not correctly fetching or preparing the data that the template expects.
- The doc variable is not being added to the context dictionary that's passed to the template.
4. Solutions
- Correct the template: If there's a typo or missing definition in the QWeb template, fix it. Ensure that doc is correctly referenced and that all necessary data is available in the template's context.
- Modify the Python code: If the Python code is not providing the correct data, adjust it to fetch the required information and pass it to the template. Make sure the doc variable (or its equivalent) is properly populated.
- Check for customizations: If you or someone else has customized the delivery slip report, review those changes carefully. Customizations are often the source of such errors.
- Update Odoo: If you're using an older version of Odoo, consider updating to the latest stable release. Bug fixes and improvements in newer versions might resolve the issue.
- Check installed modules: In some cases, this error can be caused by a faulty or incompatible module. Try disabling recently installed modules to see if that resolves the issue.
Example Scenario and Solution
Let's say the delivery slip template has this line:
<span t-esc="doc.name"/>
And the Python code preparing the data looks like this:
def _get_report_values(self, docids, data=None):
docs = self.env['stock.picking'].browse(docids)
return {
'doc_ids': docids,
'doc': docs,
}
In this case, doc is a collection of stock.picking records. To access the name of the first record, you should modify the template:
<span t-esc="doc[0].name"/>