Type of Fields¶
Basic Types
boolean:
A boolean (true, false).
Syntax:
fields.boolean('Field Name' [, Optional Parameters]),
integer:
An integer.
Syntax:
fields.integer('Field Name' [, Optional Parameters]),
float:
A floating point number.
Syntax:
fields.float('Field Name' [, Optional Parameters]),
Note
The optional parameter digits defines the precision and scale of the number. The scale being the number of digits after the decimal point whereas the precision is the total number of significant digits in the number (before and after the decimal point). If the parameter digits is not present, the number will be a double precision floating point number. Warning: these floating-point numbers are inexact (not any value can be converted to its binary representation) and this can lead to rounding errors. You should always use the digits parameter for monetary amounts.
Example:
'rate': fields.float( 'Relative Change rate', digits=(12,6) [, Optional Parameters]),
char:
A string of limited length. The required size parameter determines its size.
Syntax:
fields.char( 'Field Name', size=n [, Optional Parameters]), # where ''n'' is an integer.
Example:
'city' : fields.char('City Name', size=30, required=True),
text:
A text field with no limit in length.
Syntax:
fields.text('Field Name' [, Optional Parameters]),
date:
A date.
Syntax:
fields.date('Field Name' [, Optional Parameters]),
datetime:
Allows to store a date and the time of day in the same field.
Syntax:
fields.datetime('Field Name' [, Optional Parameters]),
binary:
A binary chain
selection:
A field which allows the user to make a selection between various predefined values.
Syntax:
fields.selection((('n','Unconfirmed'), ('c','Confirmed')), 'Field Name' [, Optional Parameters]),
Note
Format of the selection parameter: tuple of tuples of strings of the form:
(('key_or_value', 'string_to_display'), ... )
Note
You can specify a function that will return the tuple. Example
def _get_selection(self, cursor, user_id, context=None): return ( ('choice1', 'This is the choice 1'), ('choice2', 'This is the choice 2')) _columns = { 'sel' : fields.selection( _get_selection, 'What do you want ?') }
Example
Using relation fields many2one with selection. In fields definitions add:
..., 'my_field': fields.many2one( 'mymodule.relation.model', 'Title', selection=_sel_func), ...,
And then define the _sel_func like this (but before the fields definitions):
def _sel_func(self, cr, uid, context=None): obj = self.pool.get('mymodule.relation.model') ids = obj.search(cr, uid, []) res = obj.read(cr, uid, ids, ['name', 'id'], context) res = [(r['id'], r['name']) for r in res] return res