Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Combined name for a record - Odoo 8

By
Alejandro Perez Cosio
on 11/12/14, 9:14 AM 1,293 views

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

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.

Alejandro Perez Cosio
on 11/13/14, 8:17 AM
0
Alejandro Perez Cosio
On 11/13/14, 8:19 AM

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)

1
Jeff Beidler
On 11/12/14, 4:59 PM

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')

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.

Alejandro Perez Cosio
on 11/13/14, 8:25 AM

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.

Jeff Beidler
on 11/13/14, 1:34 PM
0
Dr Obx
On 4/1/15, 11:58 AM

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 ?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/12/14, 9:14 AM
Seen: 1293 times
Last updated: 4/2/15, 2:54 AM