콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
12550 화면

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

아바타
취소
베스트 답변

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

관련 게시물 답글 화면 활동
0
3월 15
4023
1
3월 15
5438
2
8월 24
2739
2
5월 22
18048
1
1월 22
3687