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

How to do it...

The my_module addon module should already have a models/library_book.py defining a basic Model. We will edit it to add new fields:

  1. Use the minimal syntax to add fields to the Library Books model:
from odoo import models, fields 
class LibraryBook(models.Model): 
    # ... 
    short_name = fields.Char('Short Title') 
    notes = fields.Text('Internal Notes') 
    state = fields.Selection( 
        [('draft', 'Not Available'), 
         ('available', 'Available'), 
         ('lost', 'Lost')], 
        'State') 
    description = fields.Html('Description') 
    cover = fields.Binary('Book Cover') 
    out_of_print = fields.Boolean('Out of Print?') 
    date_release = fields.Date('Release Date') 
    date_updated = fields.Datetime('Last Updated') 
    pages = fields.Integer('Number of Pages') 
    reader_rating = fields.Float( 
        'Reader Average Rating', 
        digits=(14, 4),  # Optional precision (total, decimals), 
    ) 
  1. All these fields support a few common attributes. As an example, we can edit the preceding pages field to add them:
    pages = fields.Integer( 
        string='Number of Pages', 
        default=0, 
        help='Total book page count', 
        groups='base.group_user', 
        states={'lost': [('readonly', True)]}, 
        copy=True, 
        index=False, 
        readonly=False, 
        required=False, 
        company_dependent=False, 
        )
  1. The Char fields support a few specific attributes. As an example, we can edit the short_name field to add them:
    short_name = fields.Char( 
        string='Short Title', 
        size=100,  # For Char only 
        translate=False,  # also for Text fields 
        ) 
  1. The HTML fields also have specific attributes:
    description = fields.Html( 
        string='Description', 
        # optional: 
        sanitize=True, 
        strip_style=False, 
        translate=False, 
    ) 

Upgrading the module will make these changes effective in the Odoo model.