Getting ready

This recipe builds on models introduced in the Write a wizard to guide the user recipe from Chapter 9, Advanced Server-Side Development Techniques, and the Extending the business logic defined in a Model recipe from Chapter 6, Basic Server-Side Development. It also uses the library.book model from Chapter 4, Creating Odoo Addon Modules.

Here's a rapid and simplified definition of the models we will use in this recipe:

from odoo import models, fields
class LibraryBook(models.Model):
_name = 'library.book'
name = fields.Char('Title', help='the title of the book', required=True)

class LibraryMember(models.Model):
_name = 'library.member'
_inherit = 'mail.thread'
name = fields.Char('Name', required=True)
email = fields.Char('Email address', required=True)
loan_ids = fields.One2many(
'library.book.loan', 'member_id')])

class LibraryBookLoan(models.Model): _name = 'library.book.loan' book_id = fields.Many2one('library.book', 'Book', required=True) member_id = fields.Many2one('library.member', 'Borrower', required=True)
expected_return_date = fields.Date('Due for', required=True)
state = fields.Selection(
[('ongoing', 'Ongoing'),
('done', 'Done')],
'State',
default='ongoing', required=True
)

You will also need to have views for these models and create some demo data. The code samples provide this.

Note that the library.member has an email field to record the email address to which we will need to send the message. We also made the model inherit from mail.thread (refer to the Adding messaging and tracking features recipe earlier in this chapter) because it is more convenient to define the template directly from the user interface.