The my_library 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 user-friendly title to the model, add the following code:
_description = 'Library Book'
- To sort the records first (from newer to older, and then by title), add the following code:
_order = 'date_release desc, name'
- To use the short_name field as the record representation, add the following code:
_rec_name = 'short_name' short_name = fields.Char('Short Title', required=True)
- Add the short_name field in the form view so that it can display the new field in the view:
<field name="short_name"/>
When we're done, our library_book.py file should look as follows:
from odoo 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', required=True) date_release = fields.Date('Release Date')
author_ids = fields.Many2many('res.partner', string='Authors')
Your <form> view in the library_book.xml file will look as follows:
<form>
<group>
<group>
<field name="name"/>
<field name="author_ids" widget="many2many_tags"/>
</group>
<group>
<field name="short_name"/>
<field name="date_release"/>
</group>
</group>
</form>
We should then upgrade the module to activate these changes in Odoo.