How to do it...

The my_library add-on module should already have 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. We have added new fields to the model. We still need to add these fields in the form view, in order to reflect these changes in the user interface. Refer to the following code to add fields in the form view:
<form>
<group>
<group>
<field name="name"/>
<field name="author_ids" widget="many2many_tags"/>
<field name="state"/>
<field name="pages"/>
<field name="notes"/>
</group>
<group>
<field name="short_name"/>
<field name="date_release"/>
<field name="date_updated"/>
<field name="cover" widget="image" class="oe_avatar"/>
<field name="reader_rating"/>
</group>
</group>
<group>
<field name="description"/>
</group>
</form>

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

Take a look at the following samples of different fields. Here, we have used different attributes on various types in the fields. This will give you a better idea of field declaration:

short_name = fields.Char('Short Title',translate=True, index=True)
state = fields.Selection(
[('draft', 'Not Available'),
('available', 'Available'),
('lost', 'Lost')],
'State', default="draft")
description = fields.Html('Description', sanitize=True, strip_style=False)
pages = fields.Integer('Number of Pages',
groups='base.group_user',
states={'lost': [('readonly', True)]},
help='Total book page count', company_dependent=False)
..................Content has been hidden....................

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