Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
2412 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
2
bře 23
3588
2
říj 19
4076
2
říj 19
3530
0
zář 17
2901
1
čvc 24
3031