对象 id 的 Django unicode 引发 TypeError:强制使用 Unicode



我有一个名为UserProfile的django模型。

这是我的代码:

user_profile = UserProfile.objects.filter(id = 1).first()
if user_profile:
user_profile_id = user_profile.id
user_details = 'user id: ' + unicode(user_profile_id)

代码运行正常,但今天我在第 4 行(最后一行(出现异常:

TypeError: coercing to Unicode: need string or buffer, NoneType found

我不知道这里可能出了什么问题。

有时unicode获取None参数, 所以像这样更改代码,

user_profile = UserProfile.objects.filter(id = 1).first()
if user_profile:
user_profile_id = user_profile.id
if user_profile_id:
user_details = 'user id: ' + unicode(user_profile_id)

相关内容

最新更新