Odoo 11 Development Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

How it works...

Fields are added to models by defining an attribute in its Python class. The non-relational field types available are as follows:

  • Char for string values.
  • Text for multi-line string values.
  • Selection for selection lists. This has a list of values and description pairs. The value that is selected is what gets stored in the database, and it can be a string or an integer. The description is automatically translatable.

While integer keys are nice, you must be aware that Odoo interprets 0  as "unset" internally and will not display the description if the stored value is zero, which can happen, so you will need to take this into account.

  • Html is similar to the text field, but is expected to store rich text in the HTML format.
  • Binary fields store binary files, such as images or documents.
  • Boolean stores True/False values.
  • Date stores date values. The ORM handles them in the string format, but they are stored in the database as dates. The format used is defined in odoo.fields.DATE_FORMAT.
  • Datetime for date-time values. They are stored in the database in a naive date time, in UTC time. The ORM represents them as a string and also in UTC time. The format used is defined in odoo.fields.DATETIME_FORMAT.
  • The Integer fields need no further explanation.
  • The Float fields store numeric values. The precision can optionally be defined with a total number of digits and decimal digits pairs.
  • Monetary can store an amount in a certain currency; it is also explained in
    another recipe.

The first step in the recipe shows the minimal syntax to add to each field type. The field definitions can be expanded to add other optional attributes, as shown in step 2.

Here's an explanation for the field attributes used:

  • string is the field's title, used in UI view labels. It's actually optional; if not set, a label will be derived from the field name by adding title case and replacing underscores with spaces.
  • size only applies to Char fields and is the maximum number of characters allowed. In general, it is advised not to use it.
  • translate, when set to True, makes the field translatable; it can hold a different value depending on the user interface language.
  • default is the default value. It can also be a function that is used to calculate the default value. For example, default=_compute_default, where _compute_default is a method defined on the model before the field definition.
  • help is an explanation text displayed in the UI tooltips.
  • groups makes the field available only to some security groups. It is a string containing a comma-separated list of XML IDs for security groups. This is addressed in more detail in Chapter 11, Access Security.
  • states allows the user interface to dynamically set the value for the readonly, required, and invisible attributes, depending on the value of the state field. Therefore, it requires a state field to exist and be used in the form view (even if it is invisible). The name of the state attribute is hardcoded in Odoo and cannot be changed.
  • copy flags whether the field value is copied when the record is duplicated. By default, it is True for non-relational and Many2one fields and False for One2many and computed fields.
  • index, when set to True, makes for the creation of a database index for the field, sometimes allowing faster searches. It replaces the deprecated select=1 attribute.
  • The readonly flag makes the field read-only by default in the user interface.
  • The required flag makes the field mandatory by default in the user interface.
  • The sanitize flag is used by HTML fields and strips its content from potentially insecure tags. Using this performs a global clean up of the input. If you need finer control, there are a few more keywords you can use, which only work if sanitize is enabled:
    • sanitize_tags=True to remove tags that are not part of a white list (this is the default)
    • sanitize_attributes=True to additionally only remove attributes that are not part of a white list
    • sanitize_style=True to remove style properties that are not part of a white list
    • strip_style=True to remove all style elements
    • strip_class=True to remove the class attributes

The various white list mentioned here are defined in odoo/tools/mail.py.

  • The company_dependent flag makes the field store different values per company. It replaces the deprecated Property field type.