python - 如何解决类型错误: cannot concatenate 'str' and 'int' objects?

标签 python python-2.7 date openerp-7

我正在尝试检索数据库中的列表。我的发言是这样的:

cr.execute("select es.* from student_resource es where es.date between '"+str(date_from)+"' and '"+str(date_to)+"'")

其中 date_fromdate_to 的类型为 fields.date 但在执行时,即我正在尝试访问我的 View ,它说:

TypeError: cannot concatenate 'str' and 'int' objects

我的陈述遗漏了什么?

日志:

File "/opt/openerp/server-7/openerp/osv/osv.py", line 187, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/opt/openerp/custom_addons/sample_new/student_resource.py", line 207, in generate_ewt_line
    from student_resource es where es.date between '"+str(date_from.strftime('%Y-%m-%d'))+"' and '"+str(date_to.strftime('%Y-%m-%d'))+"'\
TypeError: cannot concatenate 'str' and 'int' objects

这就是我得到的。

编辑

这实际上是我正在做的事情:

_logger.info("\n\t\t\tI was called. My method name is %s"%(str('generate_student_resource_report')))
for ewt in self.browse(cr,uid,ids,context):
    old_history = resource_line_obj.search(cr, uid, [('parent_id','=',ewt.id)],context=context)
    if old_history:
       ewt_line_obj.unlink(cr, uid, old_history,context=context)   
    cr.execute("insert into resource_line(partner_id,seq,base_amount,tax_amount,percent,atc_code,\
                    nature,parent_id,create_date,write_date,create_uid, write_uid)\
                select es.part_id as partner_id,\
                    (case when es.name like '%WC158%' then 1\
                        when es.name like '%WC160%' then 2\
                        when es.name like '%WC010%' then 3\
                        when es.name like '%WC140%' then 4\
                        else 0 end) as seq,\
                    sum(es.base_amt) as base_amount,\
                    sum(es.tax_amt) as tax_amount,\
                    (case when es.name like '%EWT 1\%%' then '1.00'\
                        when es.name like '%EWT 2\%%' then '2.00'\
                        when es.name like '%EWT 3\%%' then '3.00'\
                        when es.name like '%EWT 4\%%' then '4.00'\
                        when es.name like '%EWT 5\%%' then '5.00'\
                        when es.name like '%EWT 6\%%' then '6.00'\
                        when es.name like '%EWT 7\%%' then '7.00'\
                        when es.name like '%EWT 8\%%' then '8.00'\
                        when es.name like '%EWT 9\%%' then '9.00'\
                        when es.name like '%EWT 10\%%' then '10.00'\
                        else null end) as percent,\
                    (case when es.name like '%WC158%' then 'WC158'\
                        when es.name like '%WC160%' then 'WC160'\
                        when es.name like '%WC010%' then 'WC010'\
                        when es.name like '%WC140%' then 'WC140'\
                        else null end) as atc_code,\
                    (case when es.name like '%WC158%' then 'NOTEBOOK'\
                        when es.name like '%WC160%' then 'BACKPACK'\
                        when es.name like '%WC010%' then 'COLOR'\
                        when es.name like '%WC140%' then 'BOOKS' else null end) as nature,\
                    (now()) as create_date,(now()) as write_date,\
                     ? as parent_id,? as create_uid,? as write_uid\
                from student_source es where es.date between ? and ? \
                group by es.partner_id,es.name",(ewt.id,uid,uid,str(date_from),str(date_to)))
    line_list = cr.fetchall()
    _logger.info("\n\t\t\tSource ... %s"%(str(line_list)))   
    list = self.pool.get('student.resource.line').search(cr,uid,[('parent_id','=',ewt.id)])
    lines = [line.id if line.id else False for line in self.pool.get('student.resource.line').browse(cr,uid,list,context=context)]
value = {
         "value" : {
                    'name' : 'Student Resources Report',
                    "ewt_line" : lines
                    }
         }
return value

最佳答案

你应该ORM methods 。尝试这样的事情:

student_resource_obj = self.pool.get('student.resource')
lines = student_resource_obj.search(cr, uid,
    [('date', '>', date_from), 
    ('date', '<', date_to)]
)
for record in lines:
    # [...]

关于python - 如何解决类型错误: cannot concatenate 'str' and 'int' objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069071/

相关文章:

javascript - 将 JSON 从 JavaScript 返回到 Python

python - 具有多个 Pandas DataFrame 的并排箱线图

python - "cannot find vcvarsall.bat"错误后 pip 的 MinGW 编译器,仍然无法正常工作

r - 验证日期序列的顺序是否正确

java - 将日期时间字符串从一个时区 A 转换为时区 B,其中 jvm 在时区 C 中运行

python - 在 Python 列表中查找 "closest"字符串(按字母顺序)

Python - 从 Flask 中的文件上传获取文件对象的文件大小

Python:如何优化

python - 在 "OS X"上使用 Python ArgParse 的 UTF-8 STDIN+STDOUT

python - 在 Python 中为我的日期添加 1 天