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

How to override name_get?

By
Odooveloper (by Rui Franco)
on 9/4/13, 7:01 AM 3,333 views

I'm going mad with this one (or, perhaps, just going dumber):

I need to override the name_get method of project.task

It's not really important what I want to do (or if the code is clumsy), but to focus on this one annoying thing: the system totally ignores my function and it keeps calling the old (original) one.

Here's the code:

class project_task(osv.osv):
    _inherit = 'project.task'


    def name_get(self, cr, uid, ids, context=None):
            if context is None:
                context = {}

            if not ids:
                return []

            aux=''
            for r in self.read(cr, uid, ids, ['id', 'date_start', 'date_end', 'name'], context):
                if r['name']:
                    aux=r['name']
                else:
                    aux=_(r['id'])

                aux=aux + ' ('

                if r['date_start']:
                    aux=aux + _(r['date_start'])

                aux=aux + ' - '

                if r['date_end']:
                    aux=aux + _(r['date_end'])

                aux=aux + ')'

            res=(r['id'], aux)

            super(project_task, self).name_get(cr, uid, ids, context=context)
            return aux

project_task()

Why???? :)

1
Burhan Vakharia
On 1/30/16, 12:39 PM

Learn – Overriding name_get method in Odoo 8 with Example - Odoo Technical

http://odootechnical.com/overriding-name_get-method-in-odoo-8/

very good article with screenshoots now i am following your blog.

madan
on 8/24/16, 2:15 AM
0

Hi,

name_get is a function which return list of pair. The pair is the id of the item and the name the item, example [(1, "SO001"), (4, "SO004")]. It is not a string.

The super method could be call if you think that the super method return the correct item's name else you don't call super.

def name_get(self, cr, uid, ids, context=None):
    if not ids:
        return []
    res = []
    for r in self.read(cr, uid, ids, ['id', 'date_start', 'date_end', 'name'], context):
        if r['name']:
            aux = r['name']
        else:
            aux = _(r['id'])  
            # why translate a random value? You can't define all the translation

         aux +=  " ("
         if r['date_start']:
             aux += _(r['date_start']) 
             # why translate a date? I think is a mistake, the _() function must have a 
             # known string, example _("the start date is %s") % r['date_start']

         aux +=  ' - '
         if r['date_end']:
             aux += _(r['date_end']) # same question

         aux += ')'

         # aux is the name items for the r['id']
         res.append((r['id'], aux))  # append add the tuple (r['id'], aux) in the list res

    return res

I hope my answer will be helpful

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

0 follower(s)

Stats

Asked: 9/4/13, 7:01 AM
Seen: 3333 times
Last updated: 1/30/16, 12:39 PM