【问题标题】:How to display man2many fields in list view in odoo 8?如何在 odoo 8 的列表视图中显示 man2many 字段?
【发布时间】:2018-01-16 06:16:31
【问题描述】:

我尝试了两种在列表视图中显示 many2many 字段的方法,但都没有奏效:

1) <field name="test_ids"/> --> 只显示记录数。(即(3条记录))

2) <field name="test_ids" widget="many2many_tags"/> --> 显示记录的ID。 (即 2,3,4)

如何做到这一点?

【问题讨论】:

    标签: listview treeview many-to-many odoo-8 odoo


    【解决方案1】:

    正常情况

    <field name="test_ids" widget="many2many_tags"/> 
    

    必须工作。

    显示 id 的 m2m 列的原因是您的联合模型在模型中不包含 name 字段或 _rec_name 声明。

    【讨论】:

    • 它包含“名称”字段。 “many2many_tags”在表单视图的 one2many 字段中工作,但不在列表视图中。
    • @Ankit,请明确您的问题。 many2many_tags 肯定会在 many2many 字段的树视图中工作,而不是 one2many 字段。
    • Tnx。我的数据库中有一些问题。我创建了新的数据库并且它起作用了。
    【解决方案2】:

    试试这个。

    <field name="test_ids" widget="many2many_tags"/> 
    

    【讨论】:

    • 我已经尝试过了,但是没有用。它显示记录的ID。 (即 2,3,4)
    • 也许你试过widget="many2many"。你试过 widget="many2many_tags" 吗?
    • Tnx。我的数据库中有一些问题。我创建了新的数据库并且它起作用了。
    【解决方案3】:

    您似乎没有相关模型的记录名称。你有两个选择:

    • 在相关模型中添加字段名称。
    • 在相关模型上定义 _rec_name 以指向具有您要显示的名称的正确字段。

    【讨论】:

      【解决方案4】:

      要在列表视图中显示 many2many 字段,您必须使用 many2many_tags

      现在它会显示 id 列表,因为 Odoo 不知道如何表示 关系字段中的记录。

      如何告诉Odoo他如何表示记录有两种方式:

      1- 如果您的记录可以由字段的值表示,则使用rec_name 很简单 例如国家,国家的名字就足以区分记录

         class YourClass..
            _name = 'your_model_name'
            _rec_name = 'field_name' # here put the name of the field
      

      2-第二个是当一个字段不能代表记录时使用name_get

      class YourClass..
          _name = 'your_model_name'
          .....
          .....
      
          # implement this method in you model class
          @api.multi
          def name_get(self):
              """ change representation of the record by concatinating two field"""
              result = []
              for record in self:
                  result.append((record.id, _("%s %s") % (
                        record.some_field, record.some_other_field)))
              return result
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        • 2015-10-07
        • 2018-05-09
        相关资源
        最近更新 更多