gpt4 book ai didi

mysql - Odoo - 使用代码而不是 id 搜索产品

转载 作者:可可西里 更新时间:2023-11-01 08:24:19 25 4
gpt4 key购买 nike

我正在使用 odoo 10,我有两个模型 Order_Line 和 Products。

订单热线

class OrderLine(models.Model):
_name = 'order_line'
_description = 'Order Lines'

name = fields.Char()
products = fields.Many2one('amgl.products', String='Products')

产品展示
class Products(models.Model):
_name = 'products'
_description = 'Products'
_sql_constraints = [
('uniq_poduct_code', 'unique(product_code)', 'Product Code already exists!')
]

name = fields.Char()
product_code = Char()

现在我正在尝试从 csv 文件创建 order_line,并且在 csv 文件中,客户向我提供了“产品代码”而不是 Id .如何处理这个,我们使用产品代码,系统自动填充与该产品代码关联的产品。

注意:
Product Code in products 表也是唯一的,所以没有重复的机会。

CSV 模板:
customer/account_number,customer/first_name,customer/last_name,customer/account_type,order/transaction_id,order/products/product_code,order/quantity,order/customer_id/id

最佳答案

案例1 : 数据库中没有存储有客户提供给您的任何产品代码的产品

如果尚未在数据库中创建产品代码,您应该有两个 CSV 文件(Products.csv 和 OrderLine.csv)。第一个必须有三列( idnameproduct_code )。第二个也必须有三列( idnameproducts/id )。所以你只需要在 id 下创建一个 XML ID。 Products.csv 中的列,并从列的相应行中调用此 XML ID products/id文件 OrderLine.csv。

案例2 : 客户给你的产品代码属于数据库中已有的产品

现在,客户为您提供了数据库中已经存在的产品的产品代码。在这种情况下,您不必创建 Products.csv 文件。您需要知道哪些产品的 XML ID 具有客户提供给您的产品代码。为此,您可以通过 Odoo 的界面进入模型的树状 View products (如果此 View 不存在,则必须创建它)。然后,您必须选择所有记录(如果需要,请单击右上角的数字 80 以每页显示更多记录)。全部选中后,单击 More按钮和战后 Export .选择列 product_codename然后继续。例如,将生成的 CSV 文件另存为 Products.csv。打开它,您将看到导出产品的所有XML ID(如果它们没有XML ID,则在导出后他们会做-如果没有任何人,导出会为每个导出的记录生成XML ID-) .现在,我猜客户已经给了您类似的文件,其中包含订单行名称、产品代码列,因此将产品代码列值替换为您刚刚导出的产品的相应 XML ID。所以最后你应该有一个文件要导入,OrderLine.csv,id , nameproducts/id列。

案例3 : 数据库中存储了一些属于现有产品的产品代码,还有一些仍然不存在

在这种情况下,您必须将案例 1 和案例 2 结合起来,首先,按照案例 2 中的描述导出产品,然后使用代码尚不存在的产品创建一个新产品,如案例 1 中所述。然后将客户提供给您的产品代码替换为案例 2 中所述的相应代码。

备注 :如果您有数千条记录要导入并手动替换它们,则此过程将为您提供大量时间。在这种情况下,必须在 CSV 编辑器中创建一个宏来进行替换(通过搜索和替换)。例如,使用 LibreOffice,您可以使用 Python 执行宏。

Example (Case 3)

The customer has given you a file of order lines, with two lines:

  • Name: OL A, Product Code: AAA
  • Name: OL B, Product Code: BBB

You export products from Odoo interface and you get a file with one line:

id,name,product_code
__export__.products_a,"Product A","AAA"

You look for the coincidences of the product codes in both files, and do the replacements in a copy of the customer file, so now you have this:

  • Name: OL A, Product Code: __export__.products_a
  • Name: OL B, Product Code: BBB

Then you create a new CSV Products.csv and put in there the products whose product code don't exist yet:

id,name,product_code
__import__.products_b,"Product B","BBB"

Now apply the replacements again comparing this new file with the one we had, and you will get this:

  • Name: OL A, Product Code: __export__.products_a
  • Name: OL B, Product Code: __import__.products_b

Convert this file to a right CSV format for Odoo, and save it as OrderLine.csv:

id,name,products/id
__import__.order_line_1,"OL A",__export__.products_a
__import__.order_line_2,"OL B",__import__.products_b

And finally, import the files, and take into account: import Products.csv before OrderLine.csv.



编辑

我认为最好浪费一点时间为您的 CSV 编辑器(Excel、LibreOffice、Open Office 或其他)编写宏,但是如果您感到绝望并且只需要通过 Odoo 来完成此操作,我想出了有一个糟糕的解决方法,但至少,它也应该有效。

1.新建 Char字段命名 product_codeorder_line模型(它会暂时存在)。

2.修改该模型的ORM create方法:
@api.model
def create(self, vals):
product_id = False
product_code = vals.get('product_code', False)
if product_code:
product = self.env['products'].search([
('product_code', '=', product_code)
])
if product:
product_id = product[0].id
vals.update({
'products': product_id,
})
return super(OrderLine, self).create(vals)

3.复制客户发给你的文件,正确重命名标题,重命名列 order/products/product_codeproduct_code .导入 CSV 文件。每次导入记录都会调用 order_line的ORM create方法模型。

导入后,您将在数据库中拥有与产品正确相关的订单行。

完成后,您必须记住删除您添加的代码(并从数据库中的 product_code 模型中删除 order_line 列,以删除垃圾)。

关于mysql - Odoo - 使用代码而不是 id 搜索产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com