How to do it...

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:

  1. To add a user-friendly title to the model, add the following code:
_description = 'Library Book' 
  1. To sort the records first (from newer to older, and then by title), add the following code:
_order = 'date_release desc, name' 
  1. 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)
  1. 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.

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

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