How to show two different values in many2one field in Odoo

In this post you will learn how to display the value of two columns in many2one fields in OpenErp. In Odoo many2one fields mean selection field and by default value of  "name" field shows in many2one field or selection field in another module.

For example we have a module "model.section" having field named "name" and stored value of "name" in database is (A,B,C,D). Here (A,B,C,D) shows section name, And we are using it as reference in other module, named "model.register", to provide registered section detail and it will show the section name (A,B,C,D) in selection list.

Here the problem is "name" field can't be used to differentiate the section. So in this case we will override name_get method in our module. Here our goal is to see the "section name + subject name".

This method is used to display value of a record in a many2one field. Returns a textual representation for the record in self.
Return          ----->  (id , "text_represent") for each record
Return type  -----> list of tuple / list(tuple)

Now I will override name_get() method.
section_id = fields.Many2one('model.section', string="Seciton") 

1    def name_get(self, cr, uid, ids, context=None):
2        if not len(ids):
3            return []
4        res=[]
5        for section_id in self.browse(cr, uid, ids,context=context):
6            res.append((,  str(" "+str(           
7        return res 

In line no 7 here I need to prepare a list of tuple as a return value to our method. After restarting the server when I click on the many2one field of section, section name + subject name is seen as shown in the below screen shot.

No comments :

Post a Comment

Leave A Comment...