跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 新手求助
  4. one表内add many表数据,many表中的many2one字段,怎么默认选中one表id

one表内add many表数据,many表中的many2one字段,怎么默认选中one表id

已定时 已固定 已锁定 已移动 已解决 Odoo 新手求助
5 帖子 2 发布者 3.3k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • B 离线
    B 离线
    battle
    写于 最后由 编辑
    #1

    不选中可以保存,保存的时候会自动给many2one字段赋值。
    但是我想在创建的时候就选中,怎么让他默认选中one表呢,因为在创建的时候我需要用到one表的id。

    1 条回复 最后回复
    0
    • digitalsatoriD 离线
      digitalsatoriD 离线
      digitalsatori 管理员
      写于 最后由 digitalsatori 编辑
      #2

      就是在one还没出生的时候many就要嫁给他 😉
      可以在创建记录的时候用类似这样的语法创建many端的数据 {many_ids:[(0, 0, {many_field1:value1, many_field2: value2 ...})(0,0, {another line}...]}

      完整的用法参见下面的说明:

                :class:`~odoo.fields.One2many` and
                :class:`~odoo.fields.Many2many` use a special "commands" format to
                manipulate the set of records stored in/associated with the field.
      
                This format is a list of triplets executed sequentially, where each
                triplet is a command to execute on the set of records. Not all
                commands apply in all situations. Possible commands are:
      
                ``(0, _, values)``
                    adds a new record created from the provided ``value`` dict.
                ``(1, id, values)``
                    updates an existing record of id ``id`` with the values in
                    ``values``. Can not be used in :meth:`~.create`.
                ``(2, id, _)``
                    removes the record of id ``id`` from the set, then deletes it
                    (from the database). Can not be used in :meth:`~.create`.
                ``(3, id, _)``
                    removes the record of id ``id`` from the set, but does not
                    delete it. Can not be used on
                    :class:`~odoo.fields.One2many`. Can not be used in
                    :meth:`~.create`.
                ``(4, id, _)``
                    adds an existing record of id ``id`` to the set. Can not be
                    used on :class:`~odoo.fields.One2many`.
                ``(5, _, _)``
                    removes all records from the set, equivalent to using the
                    command ``3`` on every record explicitly. Can not be used on
                    :class:`~odoo.fields.One2many`. Can not be used in
                    :meth:`~.create`.
                ``(6, _, ids)``
                    replaces all existing records in the set by the ``ids`` list,
                    equivalent to using the command ``5`` followed by a command
                    ``4`` for each ``id`` in ``ids``.
      
                .. note:: Values marked as ``_`` in the list above are ignored and
                          can be anything, generally ``0`` or ``False``.
      

      【上海先安科技】(tony AT openerp.cn)

      B 1 条回复 最后回复
      0
      • B 离线
        B 离线
        battle
        在 回复了 digitalsatori 最后由 编辑
        #3

        @digitalsatori 啊,谢谢。我记得这是手动create的时候用到的语法。我直接把one2many字段放到formview里面,直接在列表里add item,然后弹框是many表字段,many表中的many2one字段不会自动赋值前面one表内容(保存之后才会赋值到字段上)。如果用到这个语法创建内容,是需要重写create方法么

        1 条回复 最后回复
        0
        • digitalsatoriD 离线
          digitalsatoriD 离线
          digitalsatori 管理员
          写于 最后由 编辑
          #4

          如果是还没有保存的话,one的记录都没有生成,怎么把id给many端?

          如果是已经保存过了,继续添加many端的记录的话,应该可以在view里定义默认值:
          <field="many_ids" context="{'default_one_side_id': active_id}"/>

          【上海先安科技】(tony AT openerp.cn)

          B 1 条回复 最后回复
          1
          • B 离线
            B 离线
            battle
            在 回复了 digitalsatori 最后由 编辑
            #5

            @digitalsatori 非常感谢,这个方法可以,one保存过之后,context可以直接定义默认值。💯

            1 条回复 最后回复
            0

            • 登录

            • 没有帐号? 注册

            • 登录或注册以进行搜索。
            • 第一个帖子
              最后一个帖子
            0
            • 版块
            • 标签
            • 热门
            • 用户
            • 群组