首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决TypeError:不能连接'str‘和'int’对象吗?

如何解决TypeError:不能连接'str‘和'int’对象吗?
EN

Stack Overflow用户
提问于 2015-08-18 09:51:10
回答 2查看 606关注 0票数 1

我正试图在我的数据库中检索一个列表。我的发言是:

代码语言:javascript
复制
cr.execute("select es.* from student_resource es where es.date between '"+str(date_from)+"' and '"+str(date_to)+"'")

如果date_fromdate_to属于fields.date类型,但在执行时,即我试图访问自己的视图时,它写道:

TypeError:无法连接“str”和“int”对象

我的陈述漏掉了什么?

日志:

代码语言:javascript
复制
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

这就是我得到的。

编辑

实际上,这就是我所做的:

代码语言:javascript
复制
_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
EN

回答 2

Stack Overflow用户

发布于 2015-08-18 10:28:30

你应该ORM方法。试着做这样的事情:

代码语言:javascript
复制
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:
    # [...]
票数 1
EN

Stack Overflow用户

发布于 2015-08-22 05:26:08

从查询中删除符号,使用%s,最后删除,并将其替换为%(ewt.date_from,ewt.date_to)

简而言之,查询看起来是,

代码语言:javascript
复制
qry = "select * from table where field1 = %s and field2=%s" %(value1, value2)
cr.execute(qry)

你的日期转换看起来像,

代码语言:javascript
复制
cr.execute("select es.* from student_resource es where es.date between '"+ date_from.strftime("%Y-%m-%d") +"' and '"+ date_to.strftime("%Y-%m-%d") +"'")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32069071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档