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

Merge 4 fields in one field in odoo7

By
philip
on 10/2/14, 12:19 PM 1,307 views

Hi all,

I have encountered weird output for my field.

when I entered
street = 85 agravento street
street2 = arebalo hills
state_id = caralvino (many2one field)
city_id = tarlac city (many2one field)

I expect the result of completeaddress field would be:
completeaddress = 85 Agravento Street Arebalo Hills Caralvino Tarlac City

but the result is:
completeaddress = 85 Agravento Street Arebalo Hills 86 196

as you can see the state_id and city_id fields became a ids instead of actual value which is "Caralvino Tarlac City"

Is there any additional line to add to my python code?

please see below XML and function

Thanks so much

 

XML----

<field name="street"/>
<field name="street2"/>
<field name="state_id" widget="selection"/>
<field name="city_id" domain="[('state_id', '=', state_id)]"/>
<field name="complete_address" invisible="1"/>

PYTHON------

class hr_employee(osv.osv):
     _inherit = "hr.employee"

    def _get_complete_address(self, cr, uid, ids, name, context=None):
        res = {}
        state_obj = self.pool['state']
        # replace 'city' with your model name
        city_obj = self.pool['city']
        for employee in self.browse(cr, uid, ids, context=context):
                street = street2 = state = city = ' '
            if employee.street:
                street = employee.street
            if employee.street2:
                street = employee.street2
            if employee.state_id:
                state = state_obj.browse(cr, uid, employee.state_id, context=context).name
            if employee.city_id:
                city = city_obj.browse(cr, uid, employee.city_id, context=context).name
        res[employee.id] = street.title() + ' ' + street2.title() + ' ' + state.title() + ' ' + city.title()
        return res

    _columns = {

        'street': fields.char('Street', size=128),
        'street2': fields.char('Street2', size=128),

        'state_id': fields.many2one('state', 'Statee'),
        'city_id': fields.many2one('cities', 'City'),

        'complete_address': fields.function(_get_complete_address, string='Complete Address', type='char', multi='_get_complete_address', store={'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['street','street2','state_id','city_id'], 10)})
    }

class state(osv.Model):

    _name = "state"
    _columns = {
        'name': fields.char('State', size=32),
    }

class city(osv.Model):
    _name = "city"
    _columns = {
        'name': fields.char('City', size=32),
        'state_id': fields.many2one('state', 'State'),
        }

ERROR----

2014-10-03 17:15:25,408 7683 INFO ABS openerp.osv.orm: storing computed values of fields.function 'complete_address'
2014-10-03 17:15:25,409 7683 ERROR ABS openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 132, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 199, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 187, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/philip/ws/openerp/my_addons/base/module/module.py", line 424, in button_immediate_install
    return self._button_immediate_function(cr, uid, ids, self.button_install, context=context)
  File "/home/philip/ws/openerp/my_addons/base/module/module.py", line 475, in _button_immediate_function
    _, pool = pooler.restart_pool(cr.dbname, update_module=True)
  File "/home/philip/ws/openerp/server/openerp/pooler.py", line 39, in restart_pool
    registry = RegistryManager.new(db_name, force_demo, status, update_module)
  File "/home/philip/ws/openerp/server/openerp/modules/registry.py", line 233, in new
    openerp.modules.load_modules(registry.db, force_demo, status, update_module)
  File "/home/philip/ws/openerp/server/openerp/modules/loading.py", line 354, in load_modules
    loaded_modules, update_module)
  File "/home/philip/ws/openerp/server/openerp/modules/loading.py", line 256, in load_marked_modules
    loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
  File "/home/philip/ws/openerp/server/openerp/modules/loading.py", line 165, in load_module_graph
    init_module_models(cr, package.name, models)
  File "/home/philip/ws/openerp/server/openerp/modules/module.py", line 385, in init_module_models
    t[1](cr, *t[2])
  File "/home/philip/ws/openerp/server/openerp/osv/orm.py", line 2853, in _update_store
    res = f.get(cr, self, iids, k, SUPERUSER_ID, {})
  File "/home/philip/ws/openerp/server/openerp/osv/fields.py", line 1142, in get
    result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
TypeError: _get_complete_address() takes at most 6 arguments (7 given)
2014-10-03 17:15:25,412 7683 ERROR ABS openerp.netsvc: _get_complete_address() takes at most 6 arguments (7 given)
Traceback (most recent call last):
  File "/home/philip/ws/openerp/server/openerp/netsvc.py", line 296, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "/home/philip/ws/openerp/server/openerp/service/web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 190, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 132, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 199, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 187, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/philip/ws/openerp/my_addons/base/module/module.py", line 424, in button_immediate_install
    return self._button_immediate_function(cr, uid, ids, self.button_install, context=context)
  File "/home/philip/ws/openerp/my_addons/base/module/module.py", line 475, in _button_immediate_function
    _, pool = pooler.restart_pool(cr.dbname, update_module=True)
  File "/home/philip/ws/openerp/server/openerp/pooler.py", line 39, in restart_pool
    registry = RegistryManager.new(db_name, force_demo, status, update_module)
  File "/home/philip/ws/openerp/server/openerp/modules/registry.py", line 233, in new
    openerp.modules.load_modules(registry.db, force_demo, status, update_module)
  File "/home/philip/ws/openerp/server/openerp/modules/loading.py", line 354, in load_modules
    loaded_modules, update_module)
  File "/home/philip/ws/openerp/server/openerp/modules/loading.py", line 256, in load_marked_modules
    loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
  File "/home/philip/ws/openerp/server/openerp/modules/loading.py", line 165, in load_module_graph
    init_module_models(cr, package.name, models)
  File "/home/philip/ws/openerp/server/openerp/modules/module.py", line 385, in init_module_models
    t[1](cr, *t[2])
  File "/home/philip/ws/openerp/server/openerp/osv/orm.py", line 2853, in _update_store
    res = f.get(cr, self, iids, k, SUPERUSER_ID, {})
  File "/home/philip/ws/openerp/server/openerp/osv/fields.py", line 1142, in get
    result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
TypeError: _get_complete_address() takes at most 6 arguments (7 given)

But will be useful to put this in write or even better to write a different function with this fields as dependents to allow to store it.

Fekete Mihai
on 10/2/14, 2:04 PM

Replace the res.country.state with state.

Fekete Mihai
on 10/3/14, 11:09 AM

I gor same error

philip
on 10/3/14, 11:47 AM
0
Fekete Mihai
On 10/2/14, 2:01 PM

You have to take the name of many2one...state_id.name.title() and the same for city, but you will have to browse this records or to read only the name.i will write to you in afew hours the complete one.

 

def _get_complete_address(self, cr, uid, ids, name, args, context=None):
    res = {}
    state_obj = self.pool['res.country.state']
    # replace 'city' with your model name
    city_obj = self.pool['city']
    for employee in self.browse(cr, uid, ids, context=context):
        street = street2 = state = city = ''
        if employee.street:
            street = employee.street
        if employee.street2:
            street = employee.street2
        if employee.state_id:
            state = state_obj.browse(cr, uid, employee.state_id, context=context).name
        if employee.city_id:
            city = city_obj.browse(cr, uid, employee.city_id, context=context).name
        res[employee.id] = street.title() + ' ' + street2.title() + ' ' + state.title() + ' ' + city.title()
    return res
    
    _columns = {
    'complete_address': fields.function(_get_complete_address, string='Complete Address', type='string', multi='_get_complete_address',
        store={
            'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['street','street2','state_id','city_id'], 10)})
    }

 

Thanks for your kind

philip
on 10/2/14, 2:12 PM

def _get_complete_address(self, cr, uid, ids, name, context=None): res = {} state_obj = self.pool['res.country.state'] # replace 'city' with your model name city_obj = self.pool['city'] for employee in self.browse(cr, uid, ids, context=context): street = street2 = state = city = '' if employee.street: street = employee.street if employee.street2: street = employee.street2 if employee.state_id: state = state_obj.browse(cr, uid, employee.state_id, context=context).name if employee.city_id: city = city_obj.browse(cr, uid, employee.city_id, context=context).name res[employee.id] = street.title() + ' ' + street2.title() + ' ' + state.title() + ' ' + city.title() return res _columns = { 'complete_address': fields.function(_get_complete_address, string='Complete Address', type='string', multi='_get_complete_address', store={ 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['street','street2','state_id','city_id'], 10)}) }

Fekete Mihai
on 10/3/14, 2:26 AM

Hi

Thanks for your effort
can you please fix the proper indent? I am missing.


----def _get_complete_address(self, cr, uid, ids, name, context=None): --------res = {}

Thanks

philip
on 10/3/14, 2:41 AM

Thank you so much

philip
on 10/3/14, 4:43 AM

You're welcome.

Fekete Mihai
on 10/3/14, 4:47 AM

Hi, I got an error message "AttributeError: 'module' object has no attribute 'string'"
please see above modified post, is my actual code

Thanks

philip
on 10/3/14, 10:53 AM

The first line after the for seems not to respect the indentation and replace the '' with ' '

Fekete Mihai
on 10/3/14, 12:06 PM

same error

philip
on 10/3/14, 12:23 PM

Sorry, my mistake...function type is char not string...in definition of it

Fekete Mihai
on 10/3/14, 12:42 PM

I got an error:
result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
TypeError: _get_complete_address() takes at most 6 arguments (7 given)
2014-10-03 17:15:25,412 7683 ERROR ABS openerp.netsvc: _get_complete_address() takes at most 6 arguments (7 given) Traceback (most recent call last):

result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
TypeError: _get_complete_address() takes at most 6 arguments (7 given)

philip
on 10/3/14, 1:17 PM

Added args in function def after name...se the answer...

Fekete Mihai
on 10/3/14, 1:34 PM

2014-10-03 18:08:22,497 11784 INFO ABS openerp.osv.orm: storing computed values of fields.function 'complete_address'
2014-10-03 18:08:22,501 11784 ERROR ABS openerp.osv.osv: Uncaught exception Traceback (most recent call last):

for field, value in result[id].iteritems():
AttributeError: 'str' object has no attribute 'iteritems'
2014-10-03 18:08:22,506 11784 ERROR ABS openerp.netsvc: 'str' object has no attribute 'iteritems' Traceback (most recent call last):

for field, value in result[id].iteritems():
AttributeError: 'str' object has no attribute 'iteritems

philip
on 10/3/14, 2:10 PM

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

2 follower(s)

Stats

Asked: 10/2/14, 12:19 PM
Seen: 1307 times
Last updated: 3/16/15, 8:10 AM