odoo_dev 开发培训作业:图书管理系统
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

58 lignes
1.9KB

  1. from odoo import api, fields, models
  2. from odoo.exceptions import Warning
  3. class Book(models.Model):
  4. _name = 'library.book'
  5. _description = 'Book'
  6. # String fields
  7. name = fields.Char('Title', required=True)
  8. isbn = fields.Char('ISBN')
  9. book_type = fields.Selection(
  10. [('paper', 'Paperback'),
  11. ('hard', 'Hardcover'),
  12. ('electronic', 'Electronic'),
  13. ('other', 'Other')],
  14. 'Type')
  15. notes = fields.Text('Internal Notes')
  16. descr = fields.Html('Description')
  17. # Numeric fields:
  18. copies = fields.Integer(default=1)
  19. avg_rating = fields.Float('Average Rating', (3,2))
  20. price = fields.Monetary('Price', 'currency_id')
  21. currency_id = fields.Many2one('res.currency') # price helper
  22. # Date and time fields
  23. date_published = fields.Date()
  24. last_borrow_date = fields.Datetime(
  25. 'Last Borrowed On',
  26. default=lambda self: fields.Datetime.now())
  27. publisher_id = fields.Many2one('res.partner', string='Publisher')
  28. author_ids = fields.Many2many('res.partner', string='Authors')
  29. # Other fields
  30. active = fields.Boolean('Active?', default=True)
  31. image = fields.Binary('Cover')
  32. # Relational Fields
  33. publisher_country_id = fields.Many2one(
  34. 'res.country', string='Publisher Country',
  35. compute='_compute_publisher_country'
  36. )
  37. @api.depends('publisher_id.country_id')
  38. def _compute_publisher_country(self):
  39. for book in self:
  40. book.publisher_country_id = book.publisher_id.country_id
  41. def button_check_isbn(self):
  42. for book in self:
  43. if not book.isbn:
  44. raise Warning('Please provide an ISBN for %s' % book.name)
  45. if book.isbn and not book._check_isbn():
  46. raise Warning('%s is an invalid ISBN' % book.isbn)
  47. return True
上海开阖软件有限公司 沪ICP备12045867号-1