fleet_vehicle_model_brand.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class FleetVehicleModelBrand(models.Model):
  5. _name = 'fleet.vehicle.model.brand'
  6. _description = 'Brand of the vehicle'
  7. _order = 'name asc'
  8. name = fields.Char('Name', required=True)
  9. image_128 = fields.Image("Logo", max_width=128, max_height=128)
  10. model_count = fields.Integer(compute="_compute_model_count", string="", store=True)
  11. model_ids = fields.One2many('fleet.vehicle.model', 'brand_id')
  12. @api.depends('model_ids')
  13. def _compute_model_count(self):
  14. model_data = self.env['fleet.vehicle.model']._read_group([
  15. ('brand_id', 'in', self.ids),
  16. ], ['brand_id'], ['brand_id'])
  17. models_brand = {x['brand_id'][0]: x['brand_id_count'] for x in model_data}
  18. for record in self:
  19. record.model_count = models_brand.get(record.id, 0)
  20. def action_brand_model(self):
  21. self.ensure_one()
  22. view = {
  23. 'type': 'ir.actions.act_window',
  24. 'view_mode': 'tree,form',
  25. 'res_model': 'fleet.vehicle.model',
  26. 'name': 'Models',
  27. 'context': {'search_default_brand_id': self.id, 'default_brand_id': self.id}
  28. }
  29. return view