Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
3549 Tampilan

Hi all,

I need your help to fix this, when I update my fields street and street2 the abs_completeadd field is not update.

When I create the record example street = "no 45 avenue street" street2= "xxxxx" my merge field abs_completeadd = "No 45 Avenue Street Xxxxx" but when I modified my fields, my abs_completeadd field is not update. I got no error, then I tried to look on the database two fields updated but my merged abs_completeadd field is not.

Thanks

    _columns = {
        'abs_street': fields.char('Street', size=128, store=False),
        'abs_street2': fields.char('Street2', size=128, store=False),
        'abs_provinces_id': fields.many2one('abs.provinces', 'Province'),
        'abs_cities_id': fields.many2one('abs.cities', 'City'),
        'abs_completeadd': fields.char('Address', size=128),
    }

def create(self, cr, uid, vals, context=None):
        abs_completeadd = str(vals['abs_street'] or '') + ' ' +str(vals['abs_street2'] or '')
        abs_street = str(vals['abs_street'] or '')
        abs_street2 = str(vals['abs_street2'] or '')
        vals['abs_completeadd'] = abs_completeadd.title()
        vals['abs_street'] = abs_street.title()
        vals['abs_street2'] = abs_street2.title()
        return super(hr_employee, self).create(cr, uid, vals, context=context)

Avatar
Buang
Jawaban Terbai

Because you only update the abs_completeadd in create method.  You need to also inherit the write method to do so.  Now, there are several things in write that is more complicated than create:

  • user may update only abs_street (in which case vals['abs_street2'] will return KeyError) or only abs_street2 (in which case vals['abs_street'] will return KeyError) or even did not update both fields (update other field that are not abs_street or abs_street2).  So, you might want to first read (or browse) the record first and then use the old value as default.  To avoid KeyError you want to use vals.get('abs_street2', '') instead of vals['abs_street2'] or ''.
  • write (can) work with multiple IDs.  So, since if abs_street2 and/or abs_street not updated you did not get the value, you want to put the necessary codes inside of the for loop that loop through all ids.
Avatar
Buang
Penulis

This is what you mean sir? I will try this later and update here. Thanks for your kind def create(self, cr, uid, vals, context=None):
abs_completeadd = str(vals.get['abs_street'] or '') + ' ' +str(vals.get['abs_street2'] or '')
abs_street = str(vals['abs_street'] or '')
abs_street2 = str(vals['abs_street2'] or '')
vals['abs_completeadd'] = abs_completeadd.title()
vals['abs_street'] = abs_street.title()
vals['abs_street2'] = abs_street2.title()
return super(hr_employee, self).create(cr, uid, vals, context=context)

You're just repeating the create method inherit that you have specified earlier. You need to inherit the write method.

Penulis

Hi John, can you give an example method inherit you've said, I am newbie in Odoo7 Thanks

What you did for method create is inheriting (overriding). You just need to do something similar to write method. You can find some examples in openerp/addons/base/res/res_partner.py itself on who to inherit write method.