简体   繁体   English

在Many2one字段的下拉列表中的Odoo名称

[英]Odoo name in dropdown for many2one field

I would like to ask about Odoo many2one field. 我想问一下Odoo many2one领域。

In 'test.project.name' model, there have 3 fields: 在“ test.project.name”模型中,有3个字段:

  • name 名称
  • prj_id prj_id
  • active 活性

Other two models used 'test.project.name' with many2one field: 另外两个模型使用了“ test.project.name”和many2one字段:

'project_id':fields.many2one('test.project.name','Project Name'),

that time the view will show 'test.project.name' model's name field data. 那时该视图将显示“ test.project.name”模型的名称字段数据。 One model is OK but I would like to show the data of prj_id filed from 'test.project.name'. 一种模式是确定的,但我想显示从“test.project.name”提起prj_id的数据。

Could I get like this? 我可以这样吗?

If you do not mind, please share some ideas. 如果您不介意,请分享一些想法。

Thanks. 谢谢。

If you need new name for all many2one('test.project.name') in the system just override method name_get . 如果您需要系统中所有many2one('test.project.name')新名称,请重写方法name_get For your model it's will be like this(you use old API): 对于您的模型,将是这样的(您使用旧的API):

class TestProject(osv.Model):
    _name = 'test.project.name'

    def name_get(self, cr, uid, ids, context):
        res = []
        for record in self.browse(cr, uid, ids, context=context):
            # As I understood prj_id it is many2one field. For example I set name of prj_id
            res.append((record.id, record.prj_id.name))
        return res

If you need use custom name for specific field you can use context to call your custom method like this: 如果需要为特定字段使用自定义名称,则可以使用context来调用自定义方法,如下所示:

<!-- in your view.xml -->
<field name="project_id" widget="selection" context="{'compute_name': '_get_my_name'}"/>

Model must looks like this: 模型必须如下所示:

class TestProject(osv.Model):
    _name = 'test.project.name'

    def name_get(self, cr, uid, ids, context=None):
        if u'compute_name' in context:
            # check value from frontend and call custom method
            return getattr(self, context[u'compute_name'])(cr, uid, ids, context)
        else:
            # call base method
            return super(TestProject, self).name_get(cr, uid, ids, context=context)

    def _get_my_name(self, cr, uid, ids, context):
        res = []
        for record in self.browse(cr, uid, ids, context=context):
            res.append((record.id, record.prj_id.name))
        return res

One more thing about this solution. 关于此解决方案的另一件事。

This way works fine only when you use widget="selection" . 只有当您使用widget="selection"时,这种方式才能正常工作。 Otherwise your custom name will use only for items in dropdown, but selected value will use default name. 否则,您的自定义名称将仅用于下拉菜单中的项目,但所选值将使用默认名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM