Momo Momoretto
Momo Momoretto

Reputation: 89

Odoo 8 what is best way to create records with reference to each other

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:

@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:

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

Upvotes: 0

Views: 1361

Answers (1)

gurney alex
gurney alex

Reputation: 13645

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.

Upvotes: 2

Related Questions