How it works...

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

  • Char is used for string values.
  • Text is used for multiline string values.
  • Selection is used 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.
In fields of the Selection type, you can use integer keys, but you must be aware that Odoo interprets 0 as unset internally and will not display the description if the stored value is zero. This 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 an HTML format.
  • Binary fields store binary files, such as images or documents.
  • Boolean stores True/False values.
  • Date stores date values. They are stored in the database as dates. The ORM handles them in the form of Python date objects. The ORM handles dates in the form of strings in version prior to Odoo 12. The format that's used is defined in odoo.fields.DATE_FORMAT.
  • Datetime is used for datetime values. They are stored in the database in a naive datetime, in UTC time. The ORM handles them in the form of Python datetime objects. The ORM handles datetime in the form of strings in the versions prior to Odoo 12. The format that's used is defined in odoo.fields.DATETIME_FORMAT.
  • The Integer fields need no further explanation.
  • The Float fields store numeric values. Their precision can optionally be defined with a total number of digits and decimal digit pairs.
  • Monetary can store an amount in a certain currency. This will also be explained in the Adding a monetary field recipe of this chapter.

The first step in this 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 that were used:

  • string is the field's title, and it is used in UI view labels. It's optional; if it is not set, a label will be derived from the field name by adding a title case and replacing the underscores with spaces.
  • 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 that was defined on the model before the field definition.
  • help is an explanation text that's 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, creates a database index for the field, which sometimes allows for 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 cleanup 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 remove attributes of the tags which 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 lists that are 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.

Finally, we updated the form view according to the newly added fields in the model. We placed <field> tags in an arbitrary manner here, but you can place them anywhere you want. Form views are explained in more detail in Chapter 10, Backend Views.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset