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.

1

Selection Field default value

By
Mathias Colpaert
on 11/11/14, 8:04 AM 7,679 views

How can you assign a default value to a selection field?

 

auType = fields.Selection([ ('type1', 'Type 1'),('type2', 'Type 2'),],'Type')

4

Maniganda

--Maniganda--
1038
| 5 7 7
Bangalore, India
--Maniganda--
Maniganda
On 11/11/14, 8:12 AM

Hi you can use default attribute in the fields

auType = fields.Selection([ ('type1', 'Type 1'),('type2', 'Type 2'),],'Type', default='type1')

1

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 11/11/14, 8:19 AM

For Version 7:

As for all fields you can define a default value for selection fields in the _default dictionary of your class.

_default = {
     'auType' = 'type1',
}

Use a key_value of the tuple of tuples of string defined in your field as default value or use any valid function:

def _get_default_type(cr, uid, context=None):
     return random.choiche(['type1', 'type2']);

Anonymous functions are also valid:

_defaults = {
     'auType': lambda self,cr,uid,ctx: random.choiche(['type1', 'type2']),
}

 

Regards

I was looking for v8 answer, but thx!

Mathias Colpaert
on 11/11/14, 8:32 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

1 follower(s)

Stats

Asked: 11/11/14, 8:04 AM
Seen: 7679 times
Last updated: 3/16/15, 8:10 AM