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:
- 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' )
- 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.