KeyError: 'id' shows when more than 2 entries are inserted

Asked by Shuaib Rahman

hi,

i'm trying to develop a new module in openerp 7.0. i have created a py file which creates the table "app_obj". when i save the first data into the table, it works fine. upon inserting the 2nd data and saving, it shows the following error:

OpenERP Server Error
Client Traceback (most recent call last):
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\http.py", line 204, in dispatch
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\controllers\main.py", line 1054, in search_read
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\controllers\main.py", line 1089, in do_search_read
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\session.py", line 42, in proxy
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\session.py", line 30, in proxy_method
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\session.py", line 103, in send

Server Traceback (most recent call last):
  File "D:\OpenERP 7.0-20130828-231133\Server\server\openerp\addons\web\session.py", line 89, in send
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\netsvc.py", line 292, in dispatch_rpc
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\service\web_services.py", line 626, in dispatch
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\osv\osv.py", line 188, in execute_kw
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\osv\osv.py", line 131, in wrapper
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\osv\osv.py", line 197, in execute
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\osv\osv.py", line 185, in execute_cr
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\osv\orm.py", line 3606, in read
  File "D:\OpenERP 7.0-20130828-231133\Server\server\.\openerp\osv\orm.py", line 3729, in _read_flat
KeyError: 2

from the postgresql, i can see that the second entry has an id 2. creating a new database and reinstalling the module does not fix it (which may mean that the error is due to any missing record). and below is the py file:

from osv import osv, fields
# import pooler
import datetime

class app_obj(osv.osv):

    count=1
    start_time=datetime.datetime.now()
    end_time=datetime.datetime.now()

    def serial(self, cr, uid, ids, arg1, arg2, context=None):
        count=self.count
        self.count+=1
        return {ids[0]:count}

    def doc_name_change1(self, cr, uid, ids, doc_name2, context=None): #this is an onChange method

        if doc_name2:
            cr.execute("""SELECT id, date_start, date_end FROM doc_info""")
            query_result=cr.dictfetchall()

            for r in query_result:
                if doc_name2 == r['id']:
                    time_start=r['date_start']
                    time_end=r['date_end']
                    self.start_time=time_start
                    self.end_time=time_end
            return {'value': {'from':time_start, 'to':time_end}}

    _name= "app_obj"

    _columns = {
               'app_id': fields.char("Appointment Ref ID", required=True),
               'date': fields.date("Date of appointment", required=True),
               'serial': fields.function(serial, method=True, type='integer', string='Serial', readonly=True),
               'doc_name': fields.many2one('doc_info', "Doctor"),
               'p_name': fields.many2one('patient_info',"Patient"),
               'app_start_time': fields.char("Start Time", required=True),
               'app_end_time': fields.char("End Time", required=True),
               'comment': fields.char("Comment"),
               'purpose': fields.many2one('purpose', 'Purpose'),
                'from': fields.datetime("Available from", readonly=True),
                'to': fields.datetime("to", readonly=True),
                'status': fields.many2one('status', "Status"),
                'rel1': fields.many2one('doc_app', "who cares"),
               }

    _defaults={
               'app_start_time': '00:00',
               'app_end_time': '00:00',
               }

app_obj()

spent a lot of time trying to find solution online, but no luck.

am developing is windows 7 platform, chrome version 29.0.1547.76 m, in case its relevant.

any help will be greatly appreciated!

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Serpent Consulting Services
Solved:
Last query:
Last reply:
Revision history for this message
Amit Parik (amit-parik) said :
#1

Its not bug rather then coding mistake, better get an answer from the question!

Revision history for this message
Best Serpent Consulting Services (serpent-consulting-services) said :
#2

Salam Shuaib,

The problem is in your serial().

It should be:

def serial(self, cr, uid, ids, arg1, arg2, context=None):
        res = {}
        count = self.count
        self.count+=1
        for rec in ids:
             res[rec] = count
        return res

I am not sure what you want to set in serial field, but the above code will fix your issue!
Thanks.

Revision history for this message
Shuaib Rahman (shuaib-q3) said :
#3

thanks a lot ppl.. silly me. couldnt see tht even after hours of wasting time on it.. and then i moved on with everything else, thinking of getting back to it sometime later.
thanks a lot :)