odoo同时创建多条记录
-
一般不会在addons模块内部来创建几千条记录这样的代码。大量记录的创建有几个方法一个是 @Siyuan 提到的用SQL来直接对数据库操作,无论是在odoo框架下用
cr.execute
执行还是直接用SQL外部工具来创建。这种方法的优势是效率高,速度快,但是弱点是会旁路Odoo对象内部的所有约束,关联操作等。多条记录创建一般的做法是使用Odoo内置的导入工具导入(
base_import
),当然如果数据比较复杂,你也可以使用xml-rpc工具导入,其原理一样是循环调用
create
方法。 -
@dodo orm 的话,除非你改框架,这涉及到default,compute 等字段的计算;
具体要参考models.py 下面单个记录的create的方法了。
@api.model @api.returns('self', lambda value: value.id) def create(self, vals): """ create(vals) -> record Creates a new record for the model. The new record is initialized using the values from ``vals`` and if necessary those from :meth:`~.default_get`. :param dict vals: values for the model's fields, as a dictionary:: {'field_name': field_value, ...} see :meth:`~.write` for details :return: new record created :raise AccessError: * if user has no create rights on the requested object * if user tries to bypass access rules for create on the requested object :raise ValidateError: if user tries to enter invalid value for a field that is not in selection :raise UserError: if a loop would be created in a hierarchy of objects a result of the operation (such as setting an object as its own parent) """ self.check_access_rights('create') # add missing defaults, and drop fields that may not be set by user vals = self._add_missing_default_values(vals) for field in itertools.chain(MAGIC_COLUMNS, ('parent_left', 'parent_right')): vals.pop(field, None) # split up fields into old-style and pure new-style ones old_vals, new_vals, unknown = {}, {}, [] for key, val in vals.iteritems(): field = self._fields.get(key) if field: if field.column or field.inherited: old_vals[key] = val if field.inverse and not field.inherited: new_vals[key] = val else: unknown.append(key) if unknown: _logger.warning("%s.create() includes unknown fields: %s", self._name, ', '.join(sorted(unknown))) # create record with old-style fields record = self.browse(self._create(old_vals)) # put the values of pure new-style fields into cache record._cache.update(record._convert_to_cache(new_vals)) # mark the fields as being computed, to avoid their invalidation for key in new_vals: self.env.computed[self._fields[key]].add(record.id) # inverse the fields for key in new_vals: self._fields[key].determine_inverse(record) for key in new_vals: self.env.computed[self._fields[key]].discard(record.id) return record
-
@digitalsatori 谢谢您的回复