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



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


  • 管理员

    就是在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``.
    


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


  • 管理员

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

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



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


登录后回复
 

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