Search code examples
pythonxmlodoo

retrieving the value of a field from another model in Odoo


please I would like to know how I can get the value of a field from another model in Odoo 12. I have a contact class and another class where I want to get the phone number of the contact class and when I use the many2one relationship it brings me the email addresses and not the phone number. I specify any time that I inherited the contact class from Odoo and I added the telephone field in this class


Solution

  • You got emails because _rec_name is set to the email field. note that emails are just displayed names, the recipient field is a reference to a mail.mass_mailing.contact record.

    To get a phone number instead of an email you can:

    • Override name_get to display phone_number:

      def name_get(self):
          res_list = []
          for contact in self:
              res_list.append((contact.id, contact.phone_number))
          return res_list  
      

      You can check if phone_number is empty and display another value instead.

    • Add a related field phone_number next to recipient field:

      recipient= fields.Many2one(comodel_name= "mail.mass_mailing.contact", required = True)
      phone_number = fields.Char(related='recipient.phone_number')