How to do it...

Odoo uses the Partner model, res.partner, to represent persons, organizations, and addresses. So, we should use it for authors and publishers. We will edit the models/library_book.py file to add these fields:

  1. Add to Library Books the many-to-one field for the book's publisher:
class LibraryBook(models.Model): 
    # ... 
    publisher_id = fields.Many2one( 
        'res.partner', string='Publisher', 
        # optional: 
        ondelete='set null', 
        context={}, 
        domain=[], 
        ) 
  1. To add the one-to-many field for a publisher's books, we need to extend the partner model. For simplicity, we will add that to the same Python file:
class ResPartner(models.Model): 
    _inherit = 'res.partner' 
    published_book_ids = fields.One2many( 
        'library.book', 'publisher_id', 
        string='Published Books') 

The _inherit attribute we use here will be explained in the Adding features to a Model using inheritance recipe later in this chapter.

  1. The many-to-many relation between books and authors was already created, but let's revisit it:
class LibraryBook(models.Model): 
    # ... 
    author_ids = fields.Many2many( 
        'res.partner', string='Authors') 
  1. The same relation, but from authors to books, should be added to the partner model:
class ResPartner(models.Model): 
    # ... 
    authored_book_ids = fields.Many2many( 
        'library.book', 
        string='Authored Books', 
        # relation='library_book_res_partner_rel'  # optional 
        ) 

Now, upgrade the addon module, and the new fields should be available in the Model. They won't be visible in views until they are added to them, but we can confirm their addition by inspecting the Model fields in Settings | Technical | Database Structure | Models.