Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

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

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

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

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

    odoo同时创建多条记录

    Odoo 新手求助
    4
    8
    5008
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dodo last edited by

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

      1 Reply Last reply Reply Quote 0
      • S
        Siyuan last edited by

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

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

        D 1 Reply Last reply Reply Quote 0
        • D
          dodo last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • D
            dodo @Siyuan last edited by

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

            S 1 Reply Last reply Reply Quote 0
            • digitalsatori
              digitalsatori 管理员 last edited by digitalsatori

              一般不会在addons模块内部来创建几千条记录这样的代码。大量记录的创建有几个方法一个是 @Siyuan 提到的用SQL来直接对数据库操作,无论是在odoo框架下用cr.execute执行还是直接用SQL外部工具来创建。这种方法的优势是效率高,速度快,但是弱点是会旁路Odoo对象内部的所有约束,关联操作等。

              多条记录创建一般的做法是使用Odoo内置的导入工具导入(base_import),

              当然如果数据比较复杂,你也可以使用xml-rpc工具导入,其原理一样是循环调用create方法。

              D 1 Reply Last reply Reply Quote 0
              • S
                Siyuan @dodo last edited by

                @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
                1 Reply Last reply Reply Quote 0
                • Joshua
                  Joshua 管理员 last edited by

                  建议调用create实现批量创建

                  1 Reply Last reply Reply Quote 0
                  • D
                    dodo @digitalsatori last edited by

                    @digitalsatori 谢谢您的回复

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post