【问题标题】:Found multiple matches for field 'Order Line'为字段“订单行”找到多个匹配项
【发布时间】:2017-12-18 13:48:48
【问题描述】:

我正在尝试将数据从 csv 导入到 Order_line 表单中,我看到了这个警告

Found multiple matches for field 'Order Line' (2 matches) between rows 2 and 6 (4 more)
  Found multiple matches for field 'Order Line' (2 matches) between rows 2 and 6
  Found multiple matches for field 'Order Line' (2 matches) between rows 2 and 6
  Found multiple matches for field 'Order Line' (2 matches) between rows 2 and 6
  Found multiple matches for field 'Order Line' (2 matches) between rows 2 and 6

因此,所有 order_lines 都是针对同一用户创建的,但是如果您看到我的 csv 的第一列是 account_number。我们有两个不同的列。

CSV

customer/account_number,customer/first_name,customer/last_name,customer/account_type,order/transaction_id,order/product_code,order/quantity
1160925,Charles L.,Richards,Segregated,10981036,G108P70NG,50
1160925,Charles L.,Richards,Segregated,10981037,G108P70NG,150
1160925,Charles L.,Richards,Segregated,10981038,G108P70NG,250
1160925,Charles L.,Richards,Segregated,10981039,G11270NG,350
1160243,"Tracy A., Jr.",Tolar,Segregated,23231554,G108P70NG,750

注意

csv 标头中的 order 实际上是 order_line 幕后我们刚刚在 csv 模板中为客户端重命名了它。

Order_line 创建方法

@api.model
def create(self, vals):
    product_id = False
    product_code = vals.get('product_code')
    if product_code:
        product = self.env['amgl.products'].search([
            ('product_code', '=', product_code)
        ])
        if product:
            product_id = product[0].id
        vals.update({
            'products': product_id,
        })
    record = super(OrderLine, self).create(vals)
    if (float(record['total_received_quantity']) > float(record['quantity'])):
        record.state = 'pending'
    return record

订单线模型

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

name = fields.Char()
customer_id = fields.Many2one('amgl.customer', string='Customer Name',
                              default=lambda self: self._context.get('customer_id', False),required=True)

导入模型

class CustodianDataImport(models.Model):
_name = 'amgl.custodian_data_import'
_description = 'Custodian Data Import'

customer = fields.One2many('amgl.customer', 'custodian_import_id', string='Customer')
order = fields.One2many('amgl.order_line', 'custodian_import_id', string='Order Line')

上面的这个模型是我进行导入的单独模型,应该从这个模型创建针对客户的所有订单。

【问题讨论】:

  • 您是否尝试直接从订单行导入客户数据?您是在使用我们在另一个问题中告诉您的方法来创建新的虚拟字段还是新的瞬态模型?
  • 如果您要覆盖 create 方法来创建订单行,然后验证客户在数据库中没有重复
  • 请在问题中添加create方法检查是否正确
  • 请看我的问题更新,我添加了所有相关代码。

标签: csv import openerp odoo-10 odoo-view


【解决方案1】:

请记住,如果您这样做:

product = self.env['amgl.products'].search([
    ('product_code', '=', product_code)
])
if product:
    product_id = product[0].id

您需要创建一个约束,使product_code 在数据库中是唯一的。如果不是,您将遇到重复问题

我建议您创建一个模型并添加要导入的字段,就像您已经完成的那样,但它应该是TransientModel,因为它只是一个辅助模型,您对在其中存储任何内容不感兴趣.

另一方面,我认为您不应该覆盖原始 order_line 模型的 create 方法。只需在辅助模型的 create 方法中添加您需要的所有内容,如下所示:

class OrderLineImport(models.TransientModel):
    _name = 'order.line.import'

    customer_name = fields.Char(
        string='Customer',
    )

    product_code = fields.Char(
        string='Product Code',
    )

    @api.model
    def create(self, vals):
        product_code = vals.get('product_code', False)
        customer_name = vals.get('customer_name', False)

        product_id = self._get_product_id(product_code)
        customer_id = self._get_customer_id(customer_name)

        order_line = {
            'product_id': product_id,
            'customer_id': customer_id
        }
        self.env['amgl.order_line'].create(order_line)   # the important create

        # You can create records of the model order.line.import as well, but they are going to disappear
        # because it is a transient model. I think that this is good to keep your database clean

        return super(OrderLineImport, self).create(vals)

    def _get_product_id(self, product_code):
        if product_code:
            product = self.env['amgl.products'].search([
                ('product_code', '=', product_code)
            ])
            if len(product) == 1:
                return product.id
            elif len(product) == 0:
                raise Warning(_('Product code not found: %s') % product_code)
            else:
                raise Warning(_('More than one product_code found: %s') % product_code)
        else:
            return False

    def _get_customer_id(self, customer_name):
        if customer_name:
            customer = self.env['amgl.customer'].search([
                ('name', '=', customer_name)
            ])
            if len(customer) == 1:
                return customer.id
            elif len(product) == 0:
                raise Warning(_('Customer not found: %s') % customer_name)
            else:
                raise Warning(_('More than one customer found: %s') % customer_name)
        else:
            return False

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多