Home Community Services Documentation

Fields

Fields define the behavior of the data on model’s record.

Field options

The following arguments are available to all field types. All are optional except Field.string.

string

Field.string

A string for the label of the field.

help

Field.help

A multi-line help string for the field.

required

Field.required

If True, the field is not allowed to be empty. Default is False.

readonly

Field.readonly

If True, the field is not editable in the client. Default is False.

domain

Field.domain

A domain constraint that will be applied on the field value.

Warning

For now it only works on relational fields like Many2One, One2Many and Many2Many.

states

Field.states

A dictionary that defines dynamic states of the field and overrides the static one. Possible keys are required, readonly and invisible. The values are PYSON statements that will be evaluated with the values of the record.

change_default

Field.change_default

If true, the field can be used as condition for a custom default value.

select

Field.select

If true, the content of the field will be indexed.

on_change

Field.on_change

A list of field names. If this attribute is set, the client will call the method on_change_<field name> of the model when the user changes the current field value and will give the values of each fields in this list. The method signature is:

on_change_<field name>(values)

This method must return a dictionary with the values of fields to be updated.

Note

The on_change_<field name> methods are runnin in a rollbacked transaction.

on_change_with

Field.on_change_with

A list of field names. Same like on_change, but defined the other way around. If this attribute is set, the client will call the method on_change_with_<field name> of the model when the user changes one of the fields defined in the list and will give the values of each fields in this list. The method signature is:

on_change_with_<field name>(values)

This method must return the new value of the field.

Note

The on_change_with_<field name> methods are running in a rollbacked transaction.

depends

Field.depends

A list of field names on which the current one depends. This means that the client will also read these fields even if they are not defined on the view. Field.depends is used per example to ensure that PYSON statement could be evaluated.

order_field

Field.order_field

The name of a substitute field on which the ordering of records must be done instead of this one. This is often used to allow ordering on Function fields.

context

Field.context

A dictionary which will update the current context when opening a relation field.

loading

Field.loading

Define how the field must be loaded: lazy or eager.

Field types

Boolean

class trytond.model.fields.Boolean(string[, **options])

A true/false field.

Integer

class trytond.model.fields.Integer(string[, **options])

An integer field.

BigInteger

class trytond.model.fields.BigInteger(string[, **options])

A long integer field.

Char

class trytond.model.fields.Char(string[, size[, translate[, **options]]])

A single line string field.

Char has two extra optional arguments:

Char.size

The maximum length (in characters) of the field. The size is enforced at the storage level and in the client input.

Char.translate

If true, the value of the field is translatable. The value readed and stored will depend on the language defined in the context.

Char.autocomplete

A list of field names. If this attribute is set, the client will call the method autocomplete_<field name> of the model when the user changes one of those field value. The method signature is:

autocomplete_<field name>(values)

This method must return a list of string that will populate the ComboboxEntry in the client.

Warning

Note that you may need to set Field.loading to lazy when Char.translate is True.

Sha

class trytond.model.fields.Sha(string[, **options])

A string field which value will be stored with a secure hash algorithm.

Text

class trytond.model.fields.Text(string[, size[, translatable[, **options]]])

A multi line string field.

Text has two extra optional arguments:

Text.size

Same as Char.size

Text.translate

Same as Char.translate

Float

class trytond.model.fields.Float(string[, digits[, **options]])

A floating-point number field. It will be represented in Python by a float instance.

Float has one extra optional arguments:

Float.digits

A tuple of two integers. The first integer defines the total of numbers in the integer part. The second integer defines the total of numbers in the decimal part. Integers can be replaced by a PYSON statement.

Numeric

class trytond.model.fields.Numeric(string[, digits[, **options]])

A fixed-point number field. It will be represented in Python by a decimal.Decimal instance.

Numeric has one extra optional arguments:

Numeric.digits

Same as Float.digits

Date

class trytond.model.fields.Date(string[, **options])

A date, represented in Python by a datetime.date instance.

DateTime

class trytond.model.fields.DateTime(string[, **options])

A date and time, represented in Python by a datetime.datetime instance.

Time

class trytond.model.fields.Time(string[, **options])

A time, represented in Python by a datetime.time instance.

Binary

class trytond.model.fields.Binary(string[, **options])

A binary field. It will be represented in Python by a str instance.

Selection

class trytond.model.fields.Selection(selection, string[, sort[, translate[, **options]]])

A string field with limited values to choice.

Selection has one extra required argument:

Selection.selection

A list of 2-tuples that looks like this:

[
    ('M', 'Male'),
    ('F', 'Female'),
]

The first element in each tuple is the actual value stored. The second element is the human-readable name.

It can also be the name of a method on the model, that will return an appropriate list. The signature of the method is:

selection()

Note

The method is automaticly added to trytond.model.Model._rpc if not manually set.

Selection has two extra optional arguments:

Selection.sort

If true, the choices will be sorted by human-readable value. Default value is True.

Selection.translate_selection

If true, the human-readable values will be translated. Default value is True.

Reference

class trytond.model.fields.Reference(string[, selection[, **options]])

A field that refers to a record of a model. It will be represented in Python by a str instance like this:

'<model name>,<record id>'

Reference has one extra optional argument:

Reference.selection

Same as Selection.selection but only for model name.

Many2One

class trytond.model.fields.Many2One(model_name, string[, left[, right[, ondelete[, datetime_field[, **options]]]]])

A many-to-one relation field.

Many2One has one extra required argument:

Many2One.model_name

The name of the target model.

Many2One has some extra optional arguments:

Many2One.left

The name of the field that stores the left value for the Modified Preorder Tree Traversal. It only works if the model_name is the same then the model.

Many2One.right

The name of the field that stores the right value. See left.

Many2One.ondelete

Define the behavior of the record when the target record is deleted. Allowed values are:

  • CASCADE: it will try to delete the record.
  • RESTRICT: it will prevent the deletion of the target record.
  • SET NULL: it will empty the relation field.

SET NULL is the default setting.

Many2One.datetime_field

If set, the target record will be read at the date defined by the datetime field name of the record. It is usually used in combination with trytond.model.ModelSQL._history to request a value for a given date and time on a historicized model.

One2Many

class trytond.model.fields.One2Many(model_name, field, string[, add_remove[, order[, datetime_field[, **options]]]])

A one-to-many relation field. It requires to have the opposite Many2One field defined on the target model.

This field accepts as written value a list of tuples like this:

  • ('create', {<field name>: value, ...}): it will create a new target record and link it to this one.
  • ('write'[, ids, ...], {<field name>: value, ...}): it will write values to target ids.
  • ('delete'[, ids, ...]): it will delete the target ids.
  • ('delete_all'): it will delete all the target records.
  • ('add'[, ids, ...]): it will link the target ids to this record.
  • ('unlink'[, ids, ...]): it will unlink the target ids from this record.
  • ('unlink_all'): it will unlink all the target records.
  • ('set'[, ids, ...]): it will link only the target ids to this record.

One2Many has some extra required arguments:

One2Many.model_name

The name of the target model.

One2Many.field

The name of the field that handles the opposite Many2One

One2Many has some extra optional arguments:

One2Many.add_remove

A domain to select records to add. If set, the client will allow to add/remove existing records instead of only create/delete.

One2Many.order

A list of tuple defining the default order of the records like for trytond.model.ModelSQL._order.

One2Many.datetime_field

Same as Many2One.datetime_field

Many2Many

class trytond.model.fields.Many2Many(relation_name, origin, target, string[, order[, datetime_field[, **options]]])

A many-to-many relation field.

Many2Many has some extra required arguments:

Many2Many.relation_name

The name of the relation model.

Many2Many.origin

The name of the field that has the Many2One to the record.

Many2Many.target

The name of the field that has the Many2One to the target record.

Many2Many has some extra optional arguments:

Many2Many.order

Same as One2Many.order

Many2Many.datetime_field

Same as Many2One.datetime_field

Instance methods:

Many2Many.get_target()

Return the target Model.

One2One

class trytond.model.fields.One2One(relation_name, origin, target, string[, datetime_field[, **options]])

A one-to-one relation field.

Warning

It is on the relation_name Model that the unicity of the couple (origin, target) must be checked.

One2One.datetime_field

Same as Many2One.datetime_field

Instance methods:

One2One.get_target()

Return the target Model.

Function

class trytond.model.fields.Function(field, getter[, setter[, searcher]])

A function field can emulate any other given field.

Function has a required argument:

Function.getter

The name of the classmethod of the Model for getting values. The signature of the method is:

getter(ids, name)

where name is the name of the field, and it must return a dictionary with a value for each ids.

Or the signature of the method is:

getter(ids, names)

where names is a list of name fields, and it must return a dictionary containing for each names a dictionary with a value for each ids.

Function has some extra optional arguments:

Function.setter

The name of the classmethod of the Model to set the value. The signature of the method id:

setter(ids, name, value)

where name is the name of the field and value the value to set.

Function.searcher

The name of the classmethod of the Model to search on the field. The signature of the method is:

searcher(name, clause)

where name is the name of the field and clause is a domain clause. It must return a list of domain clauses.

Instance methods:

Function.get(ids, model, name[, values])

Call the getter classmethod where model is the Model instance of the field, name is the name of the field.

Function.set(ids, model, name, value)

Call the setter classmethod where model is the Model instance of the field, name is the name of the field, value is the value to set.

Function.search(model, name, clause)

Call the searcher classmethod where model is the Model instance of the field, name is the name of the field, clause is a clause of domain.

Property

class trytond.model.fields.Property(field)

A property field that is like a Function field but with predifined getter, setter and searcher that use the ModelSQL ir.property to store values.

Instance methods:

Property.get(ids, model, name[, values])

Same as Function.get().

Property.set(ids, model, name, value)

Same as Function.set().

Property.search(model, name, clause)

Same as Function.search().