如何从SQL查询中提取数据并将其分配给Odoo类列?

Shr*_*tty 7 python ms-access openerp-8 odoo-8 odoo-9

我一直在尝试从.mdb数据库中提取数据并将其放入Odoo 8类列中.

这是我的.py文件

  class attendance_biometric(osv.Model):
    _name="attendance.biometric"
    _rec_name='name'
    _columns={

        'fdate':fields.datetime('From Date'),
        'tdate':fields.datetime('To Date'),
        'code':fields.integer('Code'),
        'name':fields.many2one('res.users','Employee Name', readonly=True),
        'ref': fields.one2many('bio.data', 'bio_ref', 'Data'),
    }

    _defaults = {
            'name': lambda obj, cr, uid, context: uid,

            }


def confirm_submit(self, cr, uid, ids, context=None):
        result=[]
        DBfile = '/home/administrator/test.mdb'
        conn = pyodbc.connect('DRIVER=MDBtools;DBQ='+DBfile)
        cr = conn.cursor()
        sql = '''
            select InTime, OutTime, OutDeviceId, Duration from 
AttendanceLogs '''
        cr.execute(sql)
        rows = cr.fetchall()
        for row in enumerate(rows):
            result.append(row)
        raise osv.except_osv(_('Info'),_('Data : %s\n' % (result)))
Run Code Online (Sandbox Code Playgroud)

现在,当我点击提交按钮进行一些重新工作后,数据显示如下图所示

结果记录器信息

有人可以提供宝贵的意见吗?比如如何将这些值放入Odoo类列(我的意思是分配给类的字段)以及如何从两个表中获取列.

Emi*_*td. 2

您需要了解 odoo 中的获取类型。

 - cr.dictfetchall()
       It will returns the list of dictionary.
       Example:
           [{'column1':'value_column1',}, {'column2':'value_column2',}] 

 - cr.dictfetchone() 
       It will return dictionary (Single record)
       Example:
           {'column1':'value_column1',}


 - cr.fetchall()
        It will returns the list of tuple.
        Example: 
            [('value_column1'), ('value_column2'), ].



 - cr.fetchone()
        It will returns the list of tuple.
        Example: 
            ('value_column1')
Run Code Online (Sandbox Code Playgroud)

所以更新你的代码是这样的,

res = cr.dictfetchall()
result['sname'] = res and res[0]['sname']
Run Code Online (Sandbox Code Playgroud)

无论您要设置什么值,所有这些值都必须通过查询返回。

但这只是示例,您可能需要根据您的情况进行更新。