Odoo 8什么是相互参照创建记录的最佳方式

时间:2021-07-14 14:05:30

After Ive created a method for importing external product categories into my odoo db, I had observed, that the method create of orm model needs a lot of time, when I loop for the reference to each other like this:

在我创建了一个将外部产品类别导入我的odoo db的方法后,我观察到,orm模型的方法创建需要很多时间,当我循环引用彼此时这样:

@api.multi
def action_confirm_site(self):
    account = self
    ebay_site = self.env['ebay.sites'].browse(account.site.id)
    call_data = dict(LevelLimit=3, DetailLevel='ReturnAll', CategorySiteID=ebay_site.ebay_id)
    error_msg = 'Cant get the Site Categories for site %s' % account.site
    reply = self.call(account, 'GetCategories', call_data, error_msg).response.reply
    cat_model = self.env['product.category']
    top_parent = cat_model.create({'name': account.site.name, 'type': 'ebay',
                                   'ebay_cat_id': account.site.ebay_id}).id
    ebay_categories = reply.CategoryArray.Category
    cats = []
    for category in ebay_categories:
        cat = dict(
            CategoryID=category.CategoryID,
            CategoryLevel=category.CategoryLevel,
            CategoryName=category.CategoryName,
            CategoryParentID=category.CategoryParentID
        )
        cats.append(cat.copy())
    cats.sort(key=lambda x: x['CategoryLevel'])
    id_map = {}
    for cat in cats:
        # parent_id is either the DB id of the parent, or the ROOT_ID
        if cat['CategoryParentID'] in id_map:
            parent_id = id_map[cat['CategoryParentID']]
        elif cat['CategoryParentID'] == cat['CategoryID']:
            parent_id = top_parent
        else:
            assert False, "This should not happen if the raw data is consistent"
        record = cat_model.search([('ebay_cat_id', 'in', [cat['CategoryID']])]).id
        if record:
            id_map[cat['CategoryID']] = record
            continue
        record = cat_model.create({'name': cat['CategoryName'], 'type': 'ebay', 'parent_id': parent_id,
                                   'ebay_cat_id': cat['CategoryID']}).id
        id_map[cat['CategoryID']] = record

    return

That takes a lot of time and in that case of grabbing 6 Level of Ebay categories with 16651 categories it isnt usable. Ive read that for the import method from frontend it is possible to pass external references. In my case, external references like category.CategoryParentID references to its parent with category.CategoryID. How can I change my method to create records in one loop like:

这需要花费很多时间,在这种情况下,使用16651类别获取6级Ebay类别是不可用的。我已经读过,从前端导入方法可以传递外部引用。在我的例子中,category.CategoryParentID等外部引用使用category.CategoryID引用其父类。如何在一个循环中更改我的方法来创建记录,如:

for category in ebay_categories
   cat_model.create({'id': (reference[category.CategoryID]),'name': cat['CategoryName'], 'type': 'ebay', 'parent_id': (reference[category.CategoryParentID]), 'ebay_cat_id': category.CategoryID})

1 个解决方案

#1


One likely culprit is the recomputation of the parent_left and parent_right fields. You could try disabling it by using a context with 'defer_parent_store_computation': True and then calling self._parent_store_compute(cr) when all the categories have been computed.

一个可能的罪魁祸首是重新计算parent_left和parent_right字段。您可以尝试使用带有'defer_parent_store_computation'的上下文来禁用它:True,然后在计算完所有类别后调用self._parent_store_compute(cr)。

#1


One likely culprit is the recomputation of the parent_left and parent_right fields. You could try disabling it by using a context with 'defer_parent_store_computation': True and then calling self._parent_store_compute(cr) when all the categories have been computed.

一个可能的罪魁祸首是重新计算parent_left和parent_right字段。您可以尝试使用带有'defer_parent_store_computation'的上下文来禁用它:True,然后在计算完所有类别后调用self._parent_store_compute(cr)。