Odoo Help

Welcome!

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.

2

Add preset options to a many2one field?

By
Yakito
on 2/24/13, 2:13 AM 4,643 views

Hello,

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

7

Yannick Vaucher

--Yannick Vaucher--
1699
| 5 7 9
Lausanne, Switzerland
--Yannick Vaucher--
Yannick Vaucher
On 2/24/13, 6:41 AM

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
http://doc.openerp.com/trunk/developers/server/03_module_dev_02/

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

Yakito
on 2/24/13, 11:54 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

0 follower(s)

Stats

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