Django - 强制转换为Unicode

时间:2011-09-29 15:34:19

标签: django unicode typeerror

我有一个unicode问题,因为每次我有相关的东西我都完全迷失了..

我的一个Django模板呈现了一个TypeError:

Exception Value:    

coercing to Unicode: need string or buffer, long found

给出麻烦的行只是一个字符串(我想在mysql查询中使用):

query = unicode('''(SELECT asset_name, asset_description, asset_id, etat_id, etat_name FROM Asset LEFT OUTER JOIN Etat ON etat_id_asset=asset_id WHERE asset_id_proj='''+proj+''' AND asset_id_type='''+t.type_id+''' ORDER BY asset_name, asset_description) UNION (SELECT asset_name, asset_description, asset_id, 'NULL', 'NULL' FROM Asset WHERE asset_id_proj='''+proj+''' AND asset_id_type='''+t.type_id+''' AND asset_id IN (SELECT etat_id_asset FROM Etat)); ''')

这里有什么不妥?

1 个答案:

答案 0 :(得分:2)

我知道你找到了一个更好的方法来完成,但要回答原始问题,以防你在项目的其他地方再次出现错误:

t.type_id似乎是一个长整数。除非转换为字符串,否则不能在字符串中混合使用整数,这非常简单:

myString = 'some string with type id ' + str(t.type_id) + ', and whatever else you want in the string.'