This question has been flagged
2 Replies
1183 Views

My product codes are unique, and I want to use them for importing instead of using names. How can I accomplish this? I greatly appreciate any assistance.

Avatar
Discard
Best Answer
  1. Create a Python function that performs the conversion. This function will take the product name as a parameter, search for the product based on its name, and return its internal code.
    from odoo import api, models

    class YourBOMImportModel(models.Model):
    _name = 'your.bom.import.model'

    @api.model
    def convert_product_name_to_code(self, product_name):
    product = self.env['product.product'].search([('name', '=', product_name)], limit=1)
    if product:
    return product.default_code
    return False


    In your import logic, call the convert_product_name_to_code function for each BOM line to convert the product name to its internal code.

# Assuming you have a list of BOM lines with product names

bom_lines = [{'product_name': 'Product A'}, {'product_name': 'Product B'}] bom_import_model = self.env['your.bom.import.model']

for line in bom_lines:
product_name = line['product_name']
product_code = bom_import_model.convert_product_name_to_code(product_name)

if product_code:
# Use the product code in your import logic
line['product_code'] = product_code
else:
# Handle the case when the product name doesn't exist or conversion fails
line['product_code'] = ''

In the above code, convert_product_name_to_code method performs a search on the product.product model based on the product name. It retrieves the first product that matches the name and returns its default_code field value (the internal code). If no product is found, it returns False.
You can then integrate this code into your BOM import process, where you iterate through each BOM line and call the convert_product_name_to_code function to convert the product name to its internal code. Update your BOM line data with the product code accordingly.

Avatar
Discard
Author

Thank you for your support, it means a lot to me. However, as a beginner programmer, I have limited knowledge of the Excel import process. I'm unsure about how to override the default Excel import process in Odoo. I want to import data using the 'default_code' field, but Odoo requires 'externalId' and the product name, which doesn't align with my requirements.

Best Answer

Hi  TTN SOLUTION,

I think this video can help you: https://www.youtube.com/watch?v=UOBxxnYDIsM&list=PLSKcWRTtEl5qzvRaI-VTGavfReiHS_EEb&index=1

Avatar
Discard