Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

odoo同时创建多条记录



  • 如果要创建多条记录(例如几千),只能通过多次调用create方法吗,有没有批量操作的方法



  • 可以直接cr.execute 运行psql 命令,用 insert into 增加记录。

    前提是你要对新建记录的表的结构非常熟悉



  • 此回复已被删除!


  • @siyuan 直接用orm没法实现是吗


  • 管理员

    一般不会在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

  • 管理员

    建议调用create实现批量创建



  • @digitalsatori 谢谢您的回复


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待