Skip to Content
Menu
This question has been flagged
2 Replies
9746 Views

I keep getting keyError for values in api.depends

here is the traceback:

Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/opt/odoo8/odoo/openerp/service/server.py", line 290, in app
return self.app(e, s)
File "/opt/odoo8/odoo/openerp/service/wsgi_server.py", line 216, in application
return application_unproxied(environ, start_response)
File "/opt/odoo8/odoo/openerp/service/wsgi_server.py", line 202, in application_unproxied
result = handler(environ, start_response)
File "/opt/odoo8/odoo/openerp/http.py", line 1290, in __call__
return self.dispatch(environ, start_response)
File "/opt/odoo8/odoo/openerp/http.py", line 1264, in __call__
return self.app(environ, start_wrapped)
File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 579, in __call__
return self.app(environ, start_response)
File "/opt/odoo8/odoo/openerp/http.py", line 1426, in dispatch
ir_http = request.registry['ir.http']
File "/opt/odoo8/odoo/openerp/http.py", line 346, in registry
return openerp.modules.registry.RegistryManager.get(self.db) if self.db else None
File "/opt/odoo8/odoo/openerp/modules/registry.py", line 339, in get
update_module)
File "/opt/odoo8/odoo/openerp/modules/registry.py", line 370, in new
openerp.modules.load_modules(registry._db, force_demo, status, update_module)
File "/opt/odoo8/odoo/openerp/modules/loading.py", line 357, in load_modules
registry.setup_models(cr)
File "/opt/odoo8/odoo/openerp/modules/registry.py", line 197, in setup_models
model._setup_complete(cr, SUPERUSER_ID)
File "/opt/odoo8/odoo/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo8/odoo/openerp/api.py", line 336, in old_api
result = method(recs, *args, **kwargs)
File "/opt/odoo8/odoo/openerp/models.py", line 3023, in _setup_complete
field.setup_triggers(self.env)
File "/opt/odoo8/odoo/openerp/fields.py", line 593, in setup_triggers
self._setup_dependency([], model, path.split('.'))
File "/opt/odoo8/odoo/openerp/fields.py", line 607, in _setup_dependency
fields = [model._fields[head]]
KeyError: 'hr_employee'

here is my code:

lateform.py

class late_form(osv.Model):
_name = "late.form"
_columns = {
'name': fields.char('Label',required=True,size=32),
'user_id': fields.many2one('hr.employee', 'Name', required=True),
'date': fields.datetime('Date & Time',required=True),
'reason': fields.text('Reason'),
'employee_id': fields.one2many('late.form','user_id'),
'state': fields.selection([('invalid', 'Invalid'),
('valid', 'Valid')]),
}
_defaults = {
'name': "Late report",
'date': fields.datetime.now,
'state': 'invalid',
}

def set_invalid(self, cr, uid, ids, context=None):
return self.write(cr, uid, ids, {'state': 'invalid'}, context=context)

def set_valid(self, cr, uid, ids, context=None):
return self.write(cr, uid, ids, {'state': 'valid'}, context=context)

def _send_user_id(self, cr, uid, context=None):
""" Returns ID of user
"""
if context is None:
context = {}
return {'user_id': self.browse(cr, uid, employee_id, context=context)}

class hr_employee(models.Model):
_inherit = 'hr_employee'

#what will trigger this functional field
@api.one
#events which will decide when this functional field will be updated
#when an id is added, or when the state of a sale_order_id is changed, e.g. from draft to confirmed
@api.depends('hr.employee')
def compute_late_mtd(self):
cursor = self._cr
user = self._uid
#loop through all employees in the view
for employee in self:
#define starting date and time
today = datetime.datetime.today()
#define them as strings just in case
start_date = str(today.replace(day=1).strftime('%m/%d/%Y'))
next_month = today.replace(day=28) + datetime.timedelta(days=4)
end_date = str(next_month - datetime.timedelta(days=next_month.day))
print end_date

#references the model we need to work with
employee_obj = self.pool.get('late.form')
#narrowing down and selecting all the relevant instances with specific domains
#this search returns an array
late_times = employee_obj.search(cursor, user, [('date','>=',start_date),('date','<=',end_date),('user_id','=',user.id),('state','=','valid')])
print late_times

total = 0.0
for late.form in employee_obj.browse(cursor, user, late_times):
total += 1
hr.employee.late_mtd = total

#run the function defined above and insert into a float field named late_mtd

_columns = {
'late_mtd': fields.float(compute='compute_late_mtd', store=False, string='Times late this month'),
}
Avatar
Discard
Hi Jusab,

Yes I have.

I get a keyError:
KeyError: 'hr'

On Tue, Jun 30, 2015 at 12:39 PM, Jusab Sida (jsi) <jsi@odoo.com> wrote:

Have you put "hr.employee" in depends???

--
Jusab Sida (jsi)
Sent by Tiny ERP Pvt Ltd using Odoo about Forum Post consistent keyError for api.depends

my bad....you have to put "hr" not hr.employee

Author

Hi Jusab, this works fine with hr in @api.depends but the code does not compute. Do you think there is another error somewhere?

Best Answer

Muhammad,

I see 2 places where you must enhance:

1. _inherit = 'hr_employee' should be _inherit = 'hr.employee'

2. @api.depends('hr.employee') should be @api.depends()

Also, make sure your module 'depends' on 'hr' module.

Thanks.

Avatar
Discard
Author

Thank you sir, I have tried it and the module runs fine. However it seems like the late_mtd does not compute. I am trying to just add the number of instances where a person submits a "late form" within a month. Thank you.

Ramadan kareem. In the method you should write self.late_mtd = total

Related Posts Replies Views Activity
0
Sep 24
3
0
Sep 24
347
1
Jun 24
664
1
Mar 24
914
2
Mar 24
4343