How to do it...

We will be extending the built-in Partner model. We should do this in its own Python code file, but to keep the explanation as simple as possible, we will reuse the models/library_book.py code file:

  1. First, we will ensure that the authored_book_ids inverse relation is in the Partner model and add the computed field:
class ResPartner(models.Model): 
    _inherit = 'res.partner' 
    _order = 'name' 
    authored_book_ids = fields.Many2many( 
        'library.book', string='Authored Books') 
    count_books = fields.Integer( 'Number of Authored Books', compute='_compute_count_books' ) 
  1. Next, add the method that's needed to compute the book count:
# ... 
from odoo import api  # if not already imported 
# class ResPartner(models.Model): 
    # ... 
    @api.depends('authored_book_ids') 
    def _compute_count_books(self): 
        for r in self: 
            r.count_books = len(r.authored_book_ids) 

Finally, we need to upgrade the add-on module for the modifications to take effect.

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

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