Basic field types

We will revisit the book model to present the several field types available. We should edit the library_app/models/library_book.py file and edit the Book class to look like this:

# class Book(models.Model):
# ...

# String fields: name = fields.Char('Title')
isbn = fields.Char('ISBN')
book_type = fields.Selection(
[('paper','Paperback'),
('hard','Hardcover'),
('electronic','Electronic'),
('other', 'Other')],
'Type')
notes = fields.Text('Internal Notes')
descr = fields.Html('Description')

# Numeric fields:
copies = fields.Integer(default=1)
avg_rating = fields.Float('Average Rating', (3, 2))
price = fields.Monetary('Price', 'currency_id')
currency_id = fields.Many2one('res.currency') # price helper

# Date and time fields:
date_published = fields.Date()
last_borrow_date = fields.Datetime(
'Last Borrowed On',
default=lambda self: fields.Datetime.now())

# Other fields:
active = fields.Boolean('Active?')
image = fields.Binary('Cover')

# Relational Fields
# ...to be added...

Here, we have a sample of the non-relational field types available in Odoo with the positional arguments expected by each one.

Python allows for two types of arguments: positional and keyword. Positional arguments are expected to be used in a specific order. For example, f(x, y) should be called with something like  f(1, 2). Keyword arguments are passed with the name of the argument. For the same example, we could use f(x=1, y=2), or even mix both style with f(1, y=2). More information on keyword arguments can be found in the Python official documentation at https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments.

For most non-relational fields, the first argument is the field title, corresponding to the string field argument. It is used as the default text for the user interface labels. It's optional and, if not provided, a title string will be automatically generated from the field name, replacing underscores with spaces and capitalizing the first letter in each word.

These are the non-relational field types available, along with the positional arguments expected by each:

  • Char(string) is a single line of text. The only positional argument expected is the string field label.
  • Text(string) is a multiline text. The only positional argument is the string   the field label.
  • Selection(selection, string) is a drop-down selection list. The selection positional arguments is a  [('value', 'Title'),]  list of tuples. The first tuple element is the value stored in the database. The second tuple element is the description presented in the user interface. This list can be extended by other modules using the selection_add keyword argument.
  • Html(string) is stored as a text field, but has specific handling of the user interface for HTML content presentation. For security reasons, it is sanitized by default, but this behavior can be overridden.
  • Integer(string) just expects a string argument for the field title.
  • Float(string, digits) has a second optional argument digits, an (x,y) tuple with the field's precision. x is the total number of digits; of those, y are decimal digits.
  • Monetary(string, currency_field) is similar to a float field, but has specific handling for currency. The  currency_field second argument is used to set the field storing the currency being used. By default, it is expected to be the  currency_id field.
  • Date(string) and Datetime(string) fields expect only the string text as a positional argument.
  • Boolean(string) holds True or False values, as you might expect, and only has one positional argument for the string text.
  • Binary(string) stores file-like binary data, and also expects only the string argument. It can be handled by Python code using base64 encoded strings.
Changed in Odoo 12
The Date and Datetime fields are now handled in the ORM as date objects. In previous versions, they were handled as text string representations, and, to be manipulated, needed explicit conversion to and from Python date objects.

Text fields, Char, Text, and Html, have a few specific attributes:

  • size (Char fields only) sets a maximum size allowed. It's recommended to not use it unless there is a good reason for it, for example, a social security number with a maximum length allowed.
  • translate  makes the field contents translatable, holding different values for different languages.
  • trim, set to True by default, activates automatic trimming of surrounding white space, performed by the web client. This can be explicitly disabled by setting trim=false.
Changed in Odoo 12
The trim field attribute was introduced in Odoo 12. In previous versions, text fields saved surrounding white space. 

Other than these, we also have the relational fields, which will be introduced later in this chapter. However, before that, there is still more to know about the attributes of the field types.

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

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