Search code examples
pythoncsvodoo-8odoo

How to use a record id of a CSV file for programming?


If I have CSV file that I included for seed data. Then I want to use a record inside that CSV file for programming. How can I do it in Odoo?

For example

My CSV seed file product.category.csv

id       name
ctgry_1  Custom Category

In my model I want to do this:

product_template = self.env["product.template"]
product_new = product_template.create({
    "name" : "Custom Template",
    "categ_id" : # I want to use ctgry_1 here, but it does not work
})

Should I first do a search for my category id? But that seemed like wrong to me. Shouldn't we be able to use the identifier from CSV?


Solution

  • If I understood well you want to convert some external identifier to the database id, right? Then you just need to get the id like this:

    product_template = self.env["product.template"]
    product_new = product_template.create({
        "name" : "Custom Template",
        "categ_id" : self.env.ref('ctgry_1').id
    })
    

    Anyway if you are importing data with the CSV Importing Interface you should use this format:

    "module_name.record_identifier" > "__import__.ctgry_1"
    

    You can check all the identifiers here: Settings > Sequences & Identifiers > External identifiers.

    Note: If you export data the external identifier of the record is created automatically. If you import data with the external identifier (id columns), it is assigned to the record as well