Skip to Content
Menu
This question has been flagged

Hello, everybody!

I am new at Odoo, and I am trying to make my custom module talk to the contacts' module to grab useful information for a mailing list. Below you will find the code of my solution.

Question: Is there a better way of getting the "name" field from the contact list, and together with it, automatically, the "email" field? I feel that my solution is hard to maintain and not very good for the job. Please let me know if there is a better way to do this, and thank you in advance!


models.py
​from odoo import models, fields, api

class MailingList(models.Model):
_name = 'mailing_list.mailing_list'
_description = 'Exercise #1 of Task #12'

name = fields.Many2one('res.partner', string='Name')
email = fields.Char(string='Email')

@api.model
def create(self, vals):
if vals.get('name'):
partner = self.env['res.partner'].browse(vals['name'])
vals['email'] = partner.email

result = super(MailingList, self).create(vals)
return result

def write(self, vals):
print("\n\tYou are on the method write override")
print("\tEnd of method write override\n")

if vals.get('name'):
partner = self.env['res.partner'].browse(vals['name'])
vals['email'] = partner.email

result = super(MailingList, self).write(vals)
return result



Avatar
Discard
Best Answer

Hi,

Try this to get email.

from odoo import models, fields, api


class MailingList(models.Model):

    _name = 'mailing_list.mailing_list'

    _description = 'Exercise #1 of Task #12'


    name = fields.Many2one('res.partner', string='Name')

    email = fields.Char(string='Email', related='name.email')


Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
2
Mar 23
3508
2
Oct 19
4043
2
Oct 19
3502
0
Sep 17
2873
1
Jul 24
2975