How to do it...

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

  1. Add the many-to-one field for the book's publisher to Library Books:
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 is for inheriting an existing model. This will be explained in the Adding features to a model using inheritance recipe later in this chapter.
  1. We've already created the many-to-many relation between books and authors, 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 add-on module, and the new fields should be available in the model. They won't be visible in the views until they are added to them, but we can confirm their addition by inspecting the model fields in Settings | Technical | Database Structure | Models.

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

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