Odoo Help


Add preset options to a many2one field?

on 2/24/13, 2:13 AM 5,806 views


I am developing my own module and it would be good for me to be able to show the user a many2one field which will grab its contents from a column in another table but I also need to include some preset options which will always be there.

Does anyone know if this is possible?

Thanks in advance


You are certainly looking for default values, domains or default values in the created object via a many2one. Your question could be a bit improved. So I gave you all what you might need.

Default values

In your python model you have your fields under _columns and you can also declare a dict name _defaults such as:

def _get_default_value(cr, uid, ids, context=None):
    return 'my field value'

_defaults = {
        'my_field1': _get_default_value,
        'my_field2': 'my field value',

Have a look in the addons code for more exemples. And here is the official doc about fields:

Domain restriction

For domain, you can restrict the selection for the user using the strange [('field', '=', <value>)] notation.

Just add a domain on your field.

_columns = {
        'my_field': field.many2one(..., domain=[('field', '=', <value>)]),

Setting default values in related object

And for setting values on creation of an object in a many2one, you can try to define values in the xml:

Here in a case of partners, for its contacts, the parent_id is by default set to the active id, this means the partner's contact.

<notebook colspan="4">
  <page string="Contacts" attrs="{'invisible': [('is_company', '=', False)]}">
    <field name="child_ids" context="{'default_parent_id': active_id}" mode="kanban">

In context, you must use default_XXX where XXX is the name of the field.

Objects, Fields and Methods

Thanks a lot, the last option did it for me, but the other info is really useful. Thanks for your time

on 2/24/13, 11:54 AM

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 2/24/13, 2:13 AM
Seen: 5806 times
Last updated: 3/16/15, 8:10 AM