0
votes

I would like to ask about Odoo many2one field.

In 'test.project.name' model, there have 3 fields:

  • name
  • prj_id
  • active

Other two models used 'test.project.name' with many2one field:

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

that time the view will show 'test.project.name' model's name field data. One model is OK but I would like to show the data of prj_id filed from 'test.project.name'.

Could I get like this?

If you do not mind, please share some ideas.

Thanks.

1
Your question is not too much clear . please try to make your question in depth with proper description.DASADIYA CHAITANYA

1 Answers

1
votes

If you need new name for all many2one('test.project.name') in the system just override method name_get. For your model it's will be like this(you use old 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:

<!-- 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". Otherwise your custom name will use only for items in dropdown, but selected value will use default name.