跳至内容
菜单
此问题已终结
1 回复
2944 查看

Hi all, 

I looked around but couldn't really find the full answer. I created two fields: x_studio_firstname and x_studio_lastname. Now, I need to combine these two into the x_studio_fullname field. 

I believe I have the code to do this but don't know where to put it in Odoo to fully test it. 

I read somewhere that I could change the complete_name field so that this field is generated with the custom fields. But again, I have no clue where to edit this in Odoo. Can someone point me in the right direction or provide documentation?

def name_get(self):
    res = []
    for record in self:
        name = record.x_studio_Firstname + ' ' + record.x_studio_lastname
        res.append((record.id, name))
    return res

x_studio_fullname = fields.Char(string="Full Name", compute='_compute_full_name', store=True)

@api.depends('x_studio_Firstname', 'x_studio_lastname')
def _compute_full_name(self):
    for record in self:
        record.x_studio_fullname = record.x_studio_Firstname + ' ' + record.x_studio_lastname

形象
丢弃

Would it not be easier to customize the emails and/or reports to show full name? I can't imagine your back end Users need to see the full name?

编写者

That's just it. We don't need the full name in emails or contact with customers, only the first name. If we use the standard Odoo name field, it's the full name. So we could only use that for the first name and add a last name studio field. But that doesn't seem very handy to have a contact overview to show the full name. I added the extra field in the contact layout (via studio) but that doesn't really work. We need separate fields for the first and last name and show the full name via those fields. We thought it was basic, but it seems in Odoo that this isn't standard.

最佳答案

Hello,

At Synodica, we specialize in customizing Odoo to fit your business needs. Concatenating the first and last names into a full name field is a common requirement. Here’s a step-by-step guide to achieve this in Odoo:

Step-by-Step Guide to Concatenate First and Last Names

  1. Create a Compute Field: You can create a computed field in the res.partner model to hold the full name.
  2. Modify the res.partner Model: Create a custom module or modify an existing one to add this computed field. Here’s an example of how you can do this:
from odoo import models, fields, api

class ResPartner(models.Model):
    _inherit = 'res.partner'

    full_name = fields.Char(string='Full Name', compute='_compute_full_name', store=True)

    @api.depends('name', 'last_name')
    def _compute_full_name(self):
        for partner in self:
            partner.full_name = f"{partner.name or ''} {partner.last_name or ''}".strip()

3. Update the Views: Update the views to display the new full_name field. You can add this field to the form and tree views as follows:



  1. Install/Update the Module: Install or update your custom module to apply these changes.

Explanation:

  • Computed Field: The full_name field is a computed field that depends on name (first name) and last_name. The @api.depends decorator ensures that the full_name is recomputed whenever name or last_name changes.
  • String Concatenation: The f"{partner.name or ''} {partner.last_name or ''}".strip() part concatenates the first and last names, handling any None values by substituting them with empty strings.
  • View Inheritance: The view inheritance XML adds the full_name field to the form and tree views right after the name field.

By following these steps, you’ll be able to display the full name of your contacts in Odoo.

Hope this helps!

形象
丢弃
编写者

Strange that if it's a common requirement it's not standard in Odoo. The step-by-step guide was very helpful. Thanks. I still didn't get it to work, but I now know where to look and learn this the trial and error way.

相关帖文 回复 查看 活动
8
4月 25
28800
1
3月 22
2436
3
5月 25
1584
4
5月 25
2436
2
5月 25
5772