2017-12-18 272 views
0

我试图从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_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') 

钍上面的模型是我正在执行导入的单独模型,并且从此模型中创建了针对客户的所有订单。

+0

您是否试图直接从订单行中导入客户数据?您是否正在使用我们在创建新的虚拟域或新的瞬态模型的其他问题中告诉您的方法? – ChesuCR

+0

如果您重写创建方法来创建订单行,然后验证客户没有在数据库中复制 – ChesuCR

+0

请将创建方法添加到问题以检查它是否正确 – ChesuCR

回答

0

请记住,如果你这样做:

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

你需要创建一个约束使得product_code在数据库中是唯一的。如果没有,你会有重复的问题

我建议你创建一个模型,添加你想要导入的字段,因为你已经完成了,但它应该是一个TransientModel,因为它只是一个辅助模型,而你是对存储任何东西不感兴趣。

另一方面,我认为你不应该重写原始order_line模型的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