This question has been flagged
1 Reply
2733 Views

I want to add a select list to the Product Template view which shows values from my new, supplemental table.  I found this function in the documentation:

Using relation fields many2one with selection. In fields definitions add:

..., 'my_field': fields.many2one( 'mymodule.relation.model', 'Title', selection=_sel_func), ...,

And then define the _sel_func like this (but before the fields definitions):

def _sel_func(self, cr, uid, context=None):
obj = self.pool.get('mymodule.relation.model')
ids = obj.search(cr, uid, [])
res = obj.read(cr, uid, ids, ['name', 'id'], context)
res = [(r['id'], r['name']) for r in res]
return res

This works, and I correctly get the name field in the selection list, and the database stores the record id.  However, I would like to tweak this function a bit, so that I get a concatenation of 'name' and 'description' in my selection list.  I have tried various options, but my selection list always renders exactly the same as before.  I know very little Python.  How would I modify the above function to get '[name] description' as the displayed option in my select list?

Avatar
Discard
Best Answer

Write a "name_get" method in your object " mymodule.relation.model", and by using a context (which helps ypu decide how to frame the name value), then you can display a concatenated data...

And coming to this object, in XML pass a context on the field "my_field", which will be carried to the name_get of the its model

Avatar
Discard
Author

Um... like I said, I know very little Python. Could you provide a more concrete example? I don't have much of a grasp on contexts. If I understand what you're saying about the "name_get" method (is that an API method?) it should be defined in the class from which I want to retrieve data, and would return the name + description concatenation as "name"?