Hello, I add a field *nationality* to *res.country* class and in an other form, when I use *many2one* related to *res.country*, I want to return the nationality field value instead of name value. For this purpose I inherit res.country class and I redefine name_get and name_search functions like :
class Country(osv.osv): _name = 'res.country' _inherit = 'res.country' _columns = { 'nationalite' : fields.char('Nationalite', size=64), } def name_get(self, cr, uid, ids, context={}): """ """ if not len(ids): return [] res=[] for pays in self.browse(cr, uid, ids,context=context): if context.get('country_nationalite',False) and pays.nationalite: res.append((pays.id,pays.nationalite)) return res or super(Country,self).name_get(cr,uid,ids,context) def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100): """ """ print "name_search" if not args: args=[] if not context: context={} ids = [] if context.get('country_nationalite',False): ids = self.search(cr, user, [('nationalite', operator, name)] + args, limit=limit, context=context) return self.name_get(cr, user, ids, context) else: return super(Country,self).name_search(cr, user, name, args, operator, context, limit) Country() Like you see I have a context value condition: if we have a context and the context value is True return *nationalite* value if not use name_get and name_search parent class: class teste(osv.osv): _name = 'teste' _description='teste' _columns = { 'country_id' : fields.many2one('res.country','Champ pour tester Country'), 'country_nationalite' : fields.boolean('Nationalite'), } teste() In teste form we pass country_nationalite field value in the context like this: <record model="ir.ui.view" id="view_teste_form"> <field name="name">teste.form</field> <field name="model">teste</field> <field name="type">form</field> <field name="arch" type="xml"> <form string="" > <field name="cp_ville_id"/> <field name="country_id" * context="{'country_nationalite':country_nationalite}"*/> <field name="country_nationalite"/> </form> </field> </record> <record model="ir.ui.view" id="view_teste_tree"> <field name="name">teste.tree</field> <field name="model">teste</field> <field name="type">tree</field> <field name="arch" type="xml"> <tree string="" > <field name="cp_ville_id"/> <field name="country_id" * context="{'country_nationalite':country_nationalite}"*/> <field name="country_nationalite"/> </tree> </field> </record> <record model="ir.actions.act_window" id="action_teste"> <field name="name">Testes</field> <field name="res_model">teste</field> <field name="view_type">form</field> <field name="view_mode">tree,form,calendar</field> </record> The problem is the context can not be passed in name_get and name_search function. I work on openerp-6dev. I don't know if this is a Bug on openerp-6dev or I did something wrong. Can you help me to solve this problem. Thanks, Youness
_______________________________________________ Tinyerp-users mailing list http://tiny.be/mailman2/listinfo/tinyerp-users