Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
1142 Vistas

Hello,

I'm creating xlsx report in Odoo 16 but I'm getting an error message in the logs as  "odoo.exceptions.CacheMiss: 'mrp.bom.line(459,).product_tmpl_id'" followed by "MissingError: Record does not exist or has been deleted. (Record: mrp.bom.line(459,), User: 2)" when report is printed.

My code for xlsx is: 

# -*- coding: utf-8 -*-
from odoo import models, api, _
from odoo.exceptions import UserError

class BomSheetXlsx(models.AbstractModel):
_name = 'report.report_bom_sheet_xls'
_inherit = 'report.report_xlsx.abstract'
_description = 'BOM Sheet Report'

def generate_xlsx_report(self, workbook, data, docs):
# Define formats
bold_format = workbook.add_format(
{'bold': True, 'font_size': 12, 'align': 'center', 'valign': 'vcenter', 'border': 1})
center_format = workbook.add_format({'align': 'center', 'valign': 'vcenter', 'border': 1})

# Create a worksheet
sheet = workbook.add_worksheet('BOM Sheet')

# Fallback for docs if empty
if not docs:
record_ids = data.get('active_ids', [])
docs = self.env['your_model_name'].browse(record_ids).sudo().exists() # Ensure model is correct

# Print document IDs for debugging
print("Docs after fallback and sudo:", docs.ids)

# Display Project Name
project_name = 'Unknown Project'
for obj in docs:
if hasattr(obj, 'product_tmpl_id') and obj.product_tmpl_id:
project_name = obj.product_tmpl_id.name or 'Unknown Product'
else:
project_name = 'Unknown Project'

sheet.merge_range(0, 0, 0, 3, f'Project: {project_name}', bold_format)

# Merged header for project details
sheet.merge_range('A2:D2', 'Project Details', bold_format)

# Define and write Project Lines Table Header
project_headers = [
'Product (Finished Good)',
'Product Quantity',
'Total Cost',
'Unit Cost'
]

for col_num, header in enumerate(project_headers):
sheet.write(2, col_num, header, bold_format)

# Fetch project lines, ensuring valid records exist
project_lines = self.env['mrp.project.lines'].sudo().search([('project_product_id', 'in', docs.ids)])

print("Project Lines Count:", len(project_lines)) # Debugging line

row_num = 3 # Start from row 3 for data
if project_lines:
for line in project_lines:
# Safely access fields and handle CacheMiss errors
try:
product_name = line.product_id.name if line.product_id else ''
sheet.write(row_num, 0, product_name, center_format)
sheet.write(row_num, 1, line.product_quantity, center_format)
sheet.write(row_num, 2, line.total_cost, center_format)
sheet.write(row_num, 3, line.product_unit_cost, center_format)
row_num += 1
except Exception as e:
print(f"Error accessing line data: {e}") # Log the error

# Calculate the row to start the BOM section
last_project_row = row_num
sheet.write(last_project_row, 0, '', center_format) # Blank row

# Merged header for BOM section
bom_header_row = last_project_row + 1 # Adjust row index for the BoM header
sheet.merge_range(bom_header_row, 0, bom_header_row, 10, 'Bill of Material (BoM)', bold_format)

# Define and write BOM headers
headers = [
'Finished Good', 'Semi-Finished Good', 'Raw Material', 'Qty', 'Unit of Measure',
'Std. Unit Price', 'Unit Cost', 'Ideal Qty', 'Ideal Cost', 'Order Qty', 'Min Order Qty'
]
for col_num, header in enumerate(headers):
sheet.write(bom_header_row + 1, col_num, header, bold_format)

# Fetch and write BOM lines
bom_lines = self.env['mrp.bom.line'].sudo().search([('bom_id', 'in', docs.ids)])

print("BOM Lines Count:", len(bom_lines)) # Debugging line

row_num = bom_header_row + 2 # Start from row after headers
if bom_lines:
for line in bom_lines:
try:
# Safely access fields and handle CacheMiss errors
finished_good = line.life_fg.name if line.life_fg else ''
semi_finished_good = line.model_sfg.name if line.model_sfg else ''
raw_material = line.product_id.name if line.product_id else ''
qty = line.life_qty
uom = line.product_uom_id.name if line.product_uom_id else ''
std_unit_price = line.life_uom
unit_cost = line.life_unit_cost
ideal_qty = line.product_qty
ideal_cost = line.life_ideal_cost
order_qty = line.life_order_qty
min_order_qty = line.life_min_qty

sheet.write(row_num, 0, finished_good, center_format)
sheet.write(row_num, 1, semi_finished_good, center_format)
sheet.write(row_num, 2, raw_material, center_format)
sheet.write(row_num, 3, qty, center_format)
sheet.write(row_num, 4, uom, center_format)
sheet.write(row_num, 5, std_unit_price, center_format)
sheet.write(row_num, 6, unit_cost, center_format)
sheet.write(row_num, 7, ideal_qty, center_format)
sheet.write(row_num, 8, ideal_cost, center_format)
sheet.write(row_num, 9, order_qty, center_format)
sheet.write(row_num, 10, min_order_qty, center_format)
row_num += 1
except Exception as e:
print(f"Error accessing BOM line data: {e}") # Log the error

# Adjust column widths if necessary
sheet.set_column('A:K', 23) # Adjust column width for all columns if necessary
Avatar
Descartar
Mejor respuesta

The error you're encountering (CacheMiss and MissingError) indicates that the code is trying to access a record in mrp.bom.line that does not exist or has been deleted, and Odoo’s cache is unable to find this record.

Here are some modifications and improvements that might resolve these issues:

  1. Check for Record Existence: Use the .exists() method on bom_lines to ensure you’re working only with valid, existing records. This will prevent errors if a record has been deleted or is otherwise inaccessible.
  2. Refine Access to Fields: Wrap each record access in a try-except block, as you’ve partially done, to handle any CacheMiss exceptions gracefully and ensure they are not disruptive to the report generation.
  3. Adjust Record Access for Specific Fields: product_tmpl_id might be causing the CacheMiss. Ensure any related field you’re accessing exists by using if hasattr(line, 'product_tmpl_id').

Also you are applying sudo for the search bom_lines = self.env['mrp.bom.line'].sudo().search([('bom_id', 'in', docs.ids)]), if you are in multi company env or have restricted access to some BoM's you will get teh records, but in the next iteration the odoo access check will be applied. That could be also the issue


Hope this helps

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
jun 24
1085
0
ene 25
800
1
may 22
5657
1
jul 19
4944
2
jun 23
8785