How can you assign a default value to a selection field?
auType = fields.Selection([ ('type1', 'Type 1'),('type2', 'Type 2'),],'Type')
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
How can you assign a default value to a selection field?
auType = fields.Selection([ ('type1', 'Type 1'),('type2', 'Type 2'),],'Type')
Hi you can use default attribute in the fields
auType = fields.Selection([ ('type1', 'Type 1'),('type2', 'Type 2'),],'Type', default='type1')
This is also possible
STATES = [
('draft', 'Draft'),
('confirmed', 'confirmed'),
('done', 'Done'),
]
class Foo(models.Model):
_name = 'foo.bar'
state = fields.Selection(STATES, default=STATES[0][0])
Thanks !!
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!
For version 9:
Option 1 Inline:
auType = fields.Selection(selection=[('type1', 'Type 1'),('type2', 'Type 2'),], string='Type', default='type1') Option 2 Function:
@api.model
def get_default_auType(self):
default_auType = 'type1'
return default_auType
auType = fields.Selection(selection=[('type1', 'Type 1'),('type2', 'Type 2'),], string='Type', default=get_default_auType)
Erstellen Sie heute ein Konto, um exklusive Funktionen zu nutzen und mit unserer tollen Community zu interagieren!
Registrieren
1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.