AKHIL MATHEW AKHIL MATHEW - 6 months ago 110
Python Question

Object have no attribute env error in odoo while executing a query

My code is as follows

class slotcheck(models.Model):
_name = 'slotcheck'
_inherit = ['book.meeting']
_rec_name = 'display_total'


unit_exceed = fields.Boolean('Exceeded limit',default=False)
addnl_usage = fields.Integer('Additional usage')
display_total = fields.Integer(string='Dispaly total usage', compute='_display_slot_check')
current_company = fields.Many2one('res.company','Enter your company name')

_defaults = {

'current_company': lambda obj, cr, uid, context: uid,

}



def _display_slot_check(self):
_logger = logging.getLogger(__name__)
_logger.info('#######################coming here ###########')
currentMonth = datetime.now().month
currentYear = datetime.now().year
first_day = datetime(currentYear, currentMonth, 1)
num_days = calendar.monthrange(currentYear, currentMonth)
last_day = datetime(currentYear, currentMonth, num_days[1])
conv_date = ((last_day.strftime('%Y-%m-%d'))+' '+'23:59:59')
query = "select sum(units) from book_meeting where company_name=%s and end_time>=%s and end_time<=%s " #line 41

rows_count=self._cr.execute(query,(self.current_company.id,first_day,conv_date))
data1=self._cr.fetchall()
for row in data1:
total = row
monthly = total[0]
_logger.info('#######################coming here ###########')
self.display_total = monthly


It is giving error as : slotcheck object has no attribute env while evaluating
u'self._display_slot_check()' in line 41

How can i overcome this error of attribute env not found error

Stacktrace :

Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 537, in _handle_exception
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 574, in dispatch
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 310, in _call_function
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\service\model.py", line 118, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 307, in checked_call
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 803, in __call__
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 403, in response_wrap
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\openerp\addons\web\controllers\main.py", line 1255, in run
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\http.py", line 908, in proxy
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\api.py", line 250, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\openerp\addons\base\ir\ir_actions.py", line 1002, in run
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\api.py", line 250, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\openerp\addons\base\ir\ir_actions.py", line 836, in run_action_code_multi
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\tools\safe_eval.py", line 314, in safe_eval
File "", line 1, in <module>
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\openerp\addons\stpi\models\slot_manage.py", line 39, in _display_slot_check
File "C:\Program Files (x86)\Odoo 8.0-20151015\server\.\openerp\models.py", line 5289, in <lambda>
ValueError: "'slotcheck' object has no attribute 'env'" while evaluating
u'self._display_slot_check()'

Answer

I think you have done an error. Your model name is slotcheck but you are executing query on book_meeting database as select sum(units) from book_meeting where company_name=%s and end_time>=%s and end_time<=%s

If you are executing a query on a model from a different model don't use

self._cr.execute()

instead use only

cr.execute()

Comments