This question has been flagged
1 Reply
10809 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

Avatar
Discard
Best Answer

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/

Avatar
Discard
Author

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