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账号

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

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

    Odoo 新手求助
    2
    5
    2577
    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.
    • B
      battle last edited by

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

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

        就是在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``.
        
        B 1 Reply Last reply Reply Quote 0
        • B
          battle @digitalsatori last edited by

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

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

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

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

            B 1 Reply Last reply Reply Quote 1
            • B
              battle @digitalsatori last edited by

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

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