Odoo 11 Development Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

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:

  1. To add a human-friendly title to the model, add the following:
    _description = 'Library Book' 
  1. To have records sorted, by default, first from newer to older and then by title, add the following:
    _order = 'date_release desc, name' 
  1. To use the short_name field as the record representation, add the following:
    _rec_name = 'short_name' 
    short_name = fields.Char('Short Title', required=True)

When we're done, our library_book.py file should look like this:

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')

We should then upgrade the module to have these changes activated in Odoo.