|
- from odoo import api, fields, models
- from odoo.exceptions import Warning
-
- class Book(models.Model):
- _name = 'library.book'
- _description = 'Book'
- # String fields
- name = fields.Char('Title', required=True)
- isbn = fields.Char('ISBN')
- book_type = fields.Selection(
- [('paper', 'Paperback'),
- ('hard', 'Hardcover'),
- ('electronic', 'Electronic'),
- ('other', 'Other')],
- 'Type')
- notes = fields.Text('Internal Notes')
- descr = fields.Html('Description')
-
- # Numeric fields:
- copies = fields.Integer(default=1)
- avg_rating = fields.Float('Average Rating', (3,2))
- price = fields.Monetary('Price', 'currency_id')
- currency_id = fields.Many2one('res.currency') # price helper
-
- # Date and time fields
- date_published = fields.Date()
- last_borrow_date = fields.Datetime(
- 'Last Borrowed On',
- default=lambda self: fields.Datetime.now())
-
- publisher_id = fields.Many2one('res.partner', string='Publisher')
- author_ids = fields.Many2many('res.partner', string='Authors')
-
-
-
- # Other fields
- active = fields.Boolean('Active?', default=True)
- image = fields.Binary('Cover')
-
- # Relational Fields
-
- publisher_country_id = fields.Many2one(
- 'res.country', string='Publisher Country',
- compute='_compute_publisher_country'
- )
-
- @api.depends('publisher_id.country_id')
- def _compute_publisher_country(self):
- for book in self:
- book.publisher_country_id = book.publisher_id.country_id
-
- def button_check_isbn(self):
- for book in self:
- if not book.isbn:
- raise Warning('Please provide an ISBN for %s' % book.name)
- if book.isbn and not book._check_isbn():
- raise Warning('%s is an invalid ISBN' % book.isbn)
- return True
|