Skip to main content

Posts

Showing posts with the label Extending Modules

Extending web controllers and templates

Extending web controllers and templates Extensibility is something we  expect  in all features of Odoo, and web features are no exception, so existing controllers and templates can be extended. As an example, we will extend our Book catalogue web page to leverage the book availability information we just added: On the Controller side, we will add support to a query string parameter, to filter only the available books:  /library/books?available=1 On the Template side, we will add an indication on the books that are not available Extending Web Controllers Web Controllers should not have actual  business  logic, and focus on presentation logic. We might want to add support for additional URL parameters or even routes, which are used to change the presentation of the web page. We will extend the  /library/books  endpoint to support a query string parameter,  available=1 , filtering the catalogue of book to only the availa...

Extending Python methods

Extending Python methods The business logic that's coded into Python  methods  can also be extended. For this, Odoo borrows the mechanism that Python already provides for inherited objects to extend their parent class behavior. As a practical example, we will extend the Library Book ISBN validation logic. The logic provided by the base Library app only validates modern 13-digit ISBNs, but some older titles might have a 10-digit ISBN. We will extend the  _check_isbn()  method to also validate these cases. Add this additional method in the  library_member/models/library_book.py  file: Copy from odoo import api, fields, models class Book(models.Model): _inherit = 'library.book' is_available = fields.Boolean('Is Available?', readonly=False) @api.multi def _check_isbn(self): self.ensure_one() digits = [int(x) for x in self.isbn if x.isdigit()] if len(digits) == 10: ponderators = [1, 2,...

More Model inheritance mechanisms

More Model inheritance mechanisms Previously, we saw the   basic   extension of Models, called   classic inheritance , in the official documentation. This is the most frequent use of inheritance, and the easiest way to think   about   it is as an  in-place extension . You take a Model and extend it. As you add new features, they are added to the existing Model. A new Model isn't created. We can also inherit from multiple parent Models, setting a list of values to the   _inherit   attribute. Most of the time, this is done with   mixin classes.   Mixin classes are Models that   implement   generic features to be reused. They are not expected to be used on their own, like regular Models, and are like a container of features, ready to be added to other Models. If, along with   _inherit , we also use the   _name   attribute with a value   different   from the parent Model, we get a n...