This question has been flagged
2 Replies
14116 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???? :)

Avatar
Discard
Best Answer

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

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

Avatar
Discard

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

Best Answer

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

Avatar
Discard