Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


how to add a selection field which pulls tuples/values from another table

Stefan Reichel
on 8/14/14, 5:15 AM 1,659 views



I have been trying for days to develop a module which extends crm (and later res.partner) with a field lead source.

I managed to get the datamodel amended by the model so that I now have an object res.partner.leadsource (cols: id [int], name [char], description[char], active[bool])

I'd now like to use that field in crm and make it a selection teh newly created leadsource object. The form view should only give the option to select a lead souce that's marked 'active','=','yes'.

The tupels for the selection shall be res.partner.leadsource(id,name).

I am having a hard time finding current/valid information for v7 whether to use fields.selection() or fields.many2one().

I tried both and failed. My latest approach is

class crm_lead(osv.osv):
     _inherit = "crm.lead"

     _columns = {
      'res_partner_leadsource_id': fields.many2one('res.partner.leadsource', 'contact source'),


This seems to be the same approach as in base/res/res_partner.py's "country_id" column.

I don't get an error anymore, which is great, but I am not getting the new field displayed in the Create Lead form:

The xml in the data[] section of __openerp__.py


<?xml version="1.0" encoding="utf-8"?>
    <!-- ========================This is Form layout===============================-->
    <record id="crm_case_form_view_leads_extended" model="ir.ui.view">
        <field name="name">CRM - Leads Form</field>
        <field name="model">crm.lead</field>
        <field name="inherit_id" ref="crm.crm_case_form_view_leads" />
        <field name="arch" type="xml">
            <field name="partner_name" postion="after">
            <field name="res_partner_leadsource_id" placeholder="Source" class="oe_no_button" />
<!--                <field name="res_partner_leadsource_id" widget="selection"
                   context="{'object_name': 'crm_lead'}"


Can you help me please, how to get the selection list shown on the form?

Hi René, thanks for your quick answer. I started off at the link you sent. The provided example states "selection=_sel_func" as field option. Apparently, this is not supported in v7 anymore but one should rather use fields.selection instead. I never got the example from the dev page working 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 always threw an error back at me stating that cursor had no attribute uid. So I did some more reading and found that it's recommended to do it in XML via "widget='selection' ", which I didn't get to work either. I have put the code snippet in place you sent. I understand that this would affect the filter for active=true. At this stage i cannot see the field at all. (i.e. I am at a stage prior to the filtering). How can I make the field appear? Lastly, I'd like to give users the option to add/(de-)activate lead source records  rather than me pumping them straight into the DB table. How would I do that? I do like the "create and edit" option in other selection fields, e.g. contact state. but this wouldn't give us the option to (de-)activate records.

Stefan Reichel
on 8/14/14, 5:47 AM

I tried your solution from https://www.odoo.com/forum/help-1/question/how-to-use-selection-on-many2one-46450 which doesn't give me an error anymore, but the field still doesn't show up. In debug "fields view get" shows the code has been added but it's wrapped by the partner_name field which doesn't seem right. However, if I change the XML code to I get an error about the XML not being valid for for the architecture.

Stefan Reichel
on 8/14/14, 6:22 AM

typo in the position attribute of partner_name field: position="after"

Stefan Reichel
on 8/14/14, 6:53 AM

René Schuster

--René Schuster--
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 8/14/14, 5:25 AM

In your view, remove the context and change the domain to:


I don't know the effect of class="oe_no_button" but try to omit it if you don't need it.

You can find more information on selection or many2one fields here:



Stefan Reichel
On 8/14/14, 6:57 AM

Hi Rene,

well spotted with the "position" typo. That solved it, even with "widget='selection' ". It's now visible and selectible.

I am not sure, how I managed to take over your comment. Even though your original "answer" didn't solve it, I still wanted you to get the karma, hence I marked it as answer :)



René Schuster
on 8/14/14, 6:58 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)


Asked: 8/14/14, 5:15 AM
Seen: 1659 times
Last updated: 3/16/15, 8:10 AM