Помощь

0

Skip Invalid Rows While Importing

Avatar
Muhammed Nishad

Hi Community,

Is there a way to skip the invalid rows while importing products or res.partners in odoo13?

I have gone through the base_import module several times and couldn't find anything that can help me.

Аватар
Отменить
2 Ответы
3
Best Answer

Hi,
In order to do this, you can customize the do function of base_import you need to customize the import as below.

#base import
import logging
import psycopg2
from odoo.addons.base_import.models.base_import import Import
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
def do(self, fields, columns, options, dryrun=False):
""" Actual execution of the import
:param fields: import mapping: maps each column to a field,
``False`` for the columns to ignore
:type fields: list(str|bool)
:param columns: columns label
:type columns: list(str|bool)
:param dict options:
:param bool dryrun: performs all import operations (and
validations) but rollbacks writes, allows
getting as much errors as possible without
the risk of clobbering the database.
:returns: A list of errors. If the list is empty the import
executed fully and correctly. If the list is
non-empty it contains dicts with 3 keys ``type`` the
type of error (``error|warning``); ``message`` the
error message associated with the error (a string)
and ``record`` the data which failed to import (or
``false`` if that data isn't available or provided)
:rtype: dict(ids: list(int), messages: list({type, message, record}))
"""
self.ensure_one()
self._cr.execute('SAVEPOINT import')
try:
data, import_fields = self._convert_import_data(fields, options)
# Parse date and float field
data = self._parse_import_data(data, import_fields, options)
except ValueError as error:
return {
'messages': [{
'type': 'error',
'message': str(error),
'record': False,
}]
}
_logger.info('importing %d rows...', len(data))
name_create_enabled_fields = options.pop('name_create_enabled_fields', {})
import_limit = options.pop('limit', None)
model = self.env[self.res_model].with_context(import_file=True,
name_create_enabled_fields=name_create_enabled_fields,
_import_limit=import_limit)
import_result = {
'ids': [],
'messages': [],
'nextrow': 0,
}
fails = []
for row in data:
result = {}
try:
result = model.load(import_fields, [row])
except Exception as e:
continue
error in the results.
try:
if dryrun:
self._cr.execute('ROLLBACK TO SAVEPOINT import')
# cancel all changes done to the registry/ormcache
self.pool.clear_caches()
self.pool.reset_changes()
else:
self._cr.execute('RELEASE SAVEPOINT import')
except psycopg2.InternalError:
pass
# Insert/Update mapping columns when import complete successfully
if import_result['ids'] and options.get('headers'):
BaseImportMapping = self.env['base_import.mapping']
for index, column_name in enumerate(columns):
if column_name:
# Update to latest selected field
exist_records = BaseImportMapping.search(
[('res_model', '=', self.res_model), ('column_name', '=', column_name)])
if exist_records:
exist_records.write({'field_name': fields[index]})
else:
BaseImportMapping.create({
'res_model': self.res_model,
'column_name': column_name,
'field_name': fields[index]
})
if 'name' in import_fields:
index_of_name = import_fields.index('name')
skipped = options.get('skip', 0)
# pad front as data doesn't contain anythig for skipped lines
r = import_result['name'] = [''] * skipped
# only add names for the window being imported
r.extend(x[index_of_name] for x in data[:import_limit])
# pad back (though that's probably not useful)
r.extend([''] * (len(data) - (import_limit or 0)))
else:
import_result['name'] = []
skip = options.get('skip', 0)
# convert load's internal nextrow to the imported file's
if import_result['nextrow']: # don't update if nextrow = 0 (= no nextrow)
import_result['nextrow'] += skip
return import_result
Import.do = do

Regards

Аватар
Отменить
1
Avatar
Paresh Wagh
Best Answer

Hi Muhammed: This feature is currently not available in Odoo.

1 Комментарий
Аватар
Отменить
Avatar
Muhammed Nishad
-

I appreciate your response, but actually, I'm trying to implement that feature.