You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
1000 B

  1. # Copyright (C) 2020 Open Source Integrators
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class ResPartner(models.Model):
  5. _inherit = "res.partner"
  6. @api.depends("animal_ids")
  7. def _compute_animal_count(self):
  8. for rec in self:
  9. rec.animal_count = len(rec.animal_ids)
  10. animal_ids = fields.One2many("animal", "partner_id", string="Animals")
  11. animal_count = fields.Integer(
  12. compute=_compute_animal_count, string="Number of Animals", store=True
  13. )
  14. def action_view_animals(self):
  15. xmlid = "animal.action_animal"
  16. action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
  17. if self.animal_count > 1:
  18. action["domain"] = [("id", "in", self.animal_ids.ids)]
  19. else:
  20. action["views"] = [(self.env.ref("animal.view_animal_form").id, "form")]
  21. action["res_id"] = self.animal_ids and self.animal_ids.ids[0] or False
  22. return action