0

Hay I am new to Odoo Customizing and Python and wanted to know how I can iterate through a field and take the values out of the field and put them in a new one.

The field I want to iterate through contains multiple email adresses. I want to iterate through these email adress fields, collect the email adresses and store them together in a new field.

For that I need a function.

The field I want to iterate through: My One2many field contains multiple mail adresses which I want to iterate through and collect.


field_contacts_customer_info = fields.One2many(
        'contacts.customer.information', 'another_id', string='Contacts for customer information')

The field I want to store the collected email adresses in:

selected_email = fields.Char(compute='compute_email')

This is my class: I want to collect all the email adresses from the mail_contacts field.

    _name = 'contacts.customer.information'
    _rec_name = 'name_contacts'

    name_contacts = fields.Many2one(
        'res.partner', string="Person", domain = [('is_company', '=', False)])

    mail_contacts = fields.Char(
        related = 'name_contacts.email' ,string="Email")

    another_id = fields.Many2one('res.partner', string="AnotherID")

My Try: This function collects only the last set record of the field_contacts_customer_info field and puts this record in the selected_email field of every company.So it does not work right. It should collect all the mails of the field_contacts_customer_info field for every company seperated and then put them in the selected_mail field of the belonging company.

@api.onchange('field_contacts_customer_info.mail_contacts')
def compute_email(self): 
        list_email = [] 
        for record in self: 
            if record.is_company: 
                for element in record.field_contacts_customer_info: 
                    if element.name_contacts: 
                        list_email.append(element.mail_contacts) 
                    for email in list_email: 
                        self.selected_email = email 

Thanks.

4

1 回答 1

0

您需要遍历self哪个是记录集并遍历field_contacts_customer_info字段以获取mail_contacts字段值。

@api.depends('field_contacts_customer_info.mail_contacts')
def get_email(self):
    for record in self:
        record.selected_email = ','.join(info.mail_contacts for info in record.field_contacts_customer_info if info.mail_contacts)

然后将compute属性设置为get_email

selected_email = fields.Char(string="Mail4Info", compute='get_email')

您可以查看有关如何使用计算字段的 ORM 文档。

编辑(计算方法):

您正在为 的selected_email每个元素设置 的值list_email,在compute_email执行 之后, 的值selected_email将始终是 的最后一个值list_email

每次循环时都会执行最后一个 for 循环record.field_contacts_customer_info,它应该与第二个循环处于同一级别。

list_email我们循环记录之前声明(在循环中不会重置),在第一条记录之后,每条记录将使用以前记录的电子邮件值。

record.is_company评估为False时,计算方法不会分配字段值,您应该会看到以下错误:

ValueError: Compute method failed to assign {record description}.selected_email

发生这种情况是因为计算方法必须分配一个字段值

例子:

@api.depends('field_contacts_customer_info.mail_contacts')
def compute_email(self):
    for record in self:
        list_email = []
        if record.is_company:
            for element in record.field_contacts_customer_info:
                if element.name_contacts:
                    list_email.append(element.mail_contacts)
            emails = ""
            for email in list_email:
                emails += email + " "
            record.selected_email = emails
        else:
            record.selected_email = ""

您可以将list_email类型更改为字符串并避免再次循环以获取字段值:

例子:

@api.depends('field_contacts_customer_info.mail_contacts')
def compute_email(self):
    for record in self:
        list_email = ""
        if record.is_company:
            for element in record.field_contacts_customer_info:
                if element.name_contacts:
                    list_email += element.mail_contacts
        record.selected_email = list_email
于 2021-12-02T09:21:23.153 回答