如何使用CSV文件的记录id进行编程?

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

如果我有包含种子数据的 CSV 文件。然后我想使用该 CSV 文件中的记录进行编程。我怎样才能在 Odoo 中做到这一点?

例如

我的 CSV 种子文件 product.category.csv

id       name
ctgry_1  Custom Category

在我的模型中,我想这样做:

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
})

我应该先搜索我的类别 ID 吗?但这对我来说似乎是错误的。我们不能使用 CSV 中的标识符吗?

如果我理解你想要将一些外部标识符转换为数据库 ID,对吧?然后你只需要像这样获取id:

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

无论如何,如果您使用 CSV 导入界面导入数据,您应该使用这种格式:

"module_name.record_identifier" > "__import__.ctgry_1"

您可以在此处检查所有标识符:设置 > 序列和标识符 > 外部标识符

注意:如果导出数据,则会自动创建记录的外部标识符。如果您使用外部标识符(id 列)导入数据,它也会分配给记录