
Defining the Model representation and order
Models have structural attributes defining their behavior. These are prefixed with an underscore and the most important is _name
, which defines the internal global identifier for the Model.
There are two other attributes we can use. One to set the field used as a representation, or title, for the records, and another one to set the order they are presented in.
Getting ready
This recipe assumes that you have an instance ready with my_module
, as described in Chapter 3, Creating Odoo Modules.
How to do it…
The my_module
instance should already contain a Python file called models/library_book.py
, which defines a basic model. We will edit it to add a new class-level attribute after _name
:
- To add a human-friendly title to the model, add the following:
_description = 'Library Book'
- To have records sorted first by default from newer to older and then by title, add the following:
_order = 'date_release desc, name'
- To use the
short_name
field as the record representation, add the following:_rec_name = 'short_name' short_name = fields.Char('Short Title')
When we're done, our library_book.py
file should look like this:
# -*- coding: utf-8 -*- from openerp import models, fields class LibraryBook(models.Model): _name = 'library.book' _description = 'Library Book' _order = 'date_release desc, name' _rec_name = 'short_name' name = fields.Char('Title', required=True) short_name = fields.Char('Short Title') date_release = fields.Date('Release Date')author_ids = fields.Many2many('res.partner', string='Authors')
We should then upgrade the module to have these changes activated in Odoo.
How it works…
The first step adds a friendlier description title to the model's definition. This is not mandatory, but can be used by some addons. For instance, it is used by the tracking feature in the mail
addon module for the notification text when a new record is created. For more details, see Chapter 12, Automation and Workflows.
By default, Odoo orders the records using the internal id
value. But this can be changed to use the fields of our choice by providing a _order
attribute with a string containing a comma-separated list of field names. A field name can be followed with the desc
keyword to have it sorted in reverse order.
Only fields stored in the database can be used. Non-stored computed fields can't be used to sort records.
Model records have a representation used when they are referenced from other records. For example, a user_id
field with the value 1 represents the Administrator user. When displayed in a form view, we will be shown the user name rather than the database ID. By default, the name
field is used. In fact, that is the default value for the _rec_name
attribute, and that's why it's convenient to have a name
field in our Models.
If no name
field exists in the model, a representation is generated with the model and record identifiers, similar to (library.book, 1).
There's more…
Record representation is available in a magic display_name
computed field added automatically to all models since version 8.0. Its values are generated using the Model method name_get()
, which was already in existence in previous Odoo versions.
Its default implementation uses the _rec_name
attribute. For more sophisticated representations, we can override its logic. This method must return a list of tuples with two elements: the ID of the record and the Unicode string representation for the record.
For example, to have the title and its release date in the representation, such as Moby Dick (1851-10-18), we could define the following:
def name_get(self): result = [] for record in self: result.append( (record.id, u"%s (%s)" % (record.name, record.date_released) )) return result
Do notice that we used a Unicode string while building the record representation, u"%s (%s)"
. This is important to avoid errors, in case we find non-ASCII characters.