Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Combined name for a record - Odoo 8

Subscribe

Get notified when there's activity on this post

This question has been flagged
record_name
6 Replies
5304 Views
Avatar
Alejandro Perez Cosio

Hello, 
I'm triying to set a default record name as a combination of 2 fields: 
I have a table with 3 columns (just for the example): 

  • name
  • start_date
  • end_date


And I would want to have the name calculated with the following:

def _get_name(...):
    element = self.browse(cr, uid, ids, context=context)
    return '%s -> %s' % (element.start_date, element.end_date)

_columns = {
    'name': fields.function(_get_name, type='char'...)
    ...
}

How can I get that?

Thank you in advance

0
Avatar
Discard
Alejandro Perez Cosio
Author

The problem here is I wasn't iterating on the element (which should be elements). A res list should also be declared as empty and append the custom name to this list with the record id.

Avatar
Alejandro Perez Cosio
Author Best Answer

This is how I finally got my combined name:

def _get_name(self, cr, uid, ids, name, arg, context=None):
        reads = self.read(cr, uid, ids, ['start_date','end_date'], context=context)
        res = []
        for record in reads:
            name = '%s -> %s' % (record['start_date'], record['end_date'])
            res.append((record['id'], name))
        return res

And for the field:

'name': fields.function(_get_name, type='char', store=True)

0
Avatar
Discard
Avatar
Jeff Beidler
Best Answer

Hi Alejandro,

Here is the function I used recently to retrieve a combination of two fields as the name:

   @api.multi
    def name_get(self):
        
        retvals = []
        
        for record in self:
            if self.env.context.get('prod_form', False):
                catstring =  "[" + record.name + "] " + record.description
                retvals.append((record.id,catstring))
            else:
                retvals.append((record.id,record.name))
       
        return retvals

In this case, it returns the combination of 'name' and 'description' only when the 'prod_form' context is true.  It should steer you in the right direction, though, as it uses the new v8 API form.  Notice that the function only needs 'self' as a parameter.  All the other variables (cr, uid, ids, context, etc.) are wrapped up in the environment, which is part of 'self'. 

In this post, Marvin helped me understand what (I hope) I explained above.  My original question also contains a link to the API documentation, if you haven't found that already.

I think you also want to declare your field like this:

name = fields.Char(compute='yourFunction_name')

1
Avatar
Discard
Alejandro Perez Cosio
Author

Thank you Jeff, your answer helps me to understand a little more how odoo works, I'm always building modules with a dictionary for _columns but I will try this on my next module.

Jeff Beidler

That still works. You'll find that Odoo still has a mix of old and new API stuff. That tripped me up for a while, until Marvin pointed out (see linked post) that I needed the @api.multi decorator on my name_get function.

Avatar
Dr Obx
Best Answer

I was looking for something like this recently and suddenly found it but would anyone tell me, I need it for product so if possible to make some changes to make it work with product please?

I need combine 3/4 fields together so as a result the product name should contain brand, model, size,  ...

Can anyone show me exactly how should it look like for product module ?

0
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now