Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
12222 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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/

Awatar
Odrzuć
Autor

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

Powiązane posty Odpowiedzi Widoki Czynność
0
mar 15
3715
1
mar 15
5124
2
sie 24
2288
2
maj 22
18048
1
sty 22
3356