Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:无法连接';str';和';长';物体_Python - Fatal编程技术网

python:无法连接';str';和';长';物体

python:无法连接';str';和';长';物体,python,Python,我试图在django中设置一个选择字段,但我认为这不是django的问题。choices字段使用2元组的iterable(例如,列表或元组)作为该字段的选项 这是我的密码: self.fields['question_' + question.id] = forms.ChoiceField( label=question.label, help_text=question.description, r

我试图在django中设置一个选择字段,但我认为这不是django的问题。choices字段使用2元组的iterable(例如,列表或元组)作为该字段的选项

这是我的密码:

self.fields['question_' + question.id] = forms.ChoiceField(
                label=question.label,
                help_text=question.description,
                required=question.answer_set.required,
                choices=[("fe", "a feat"), ("faaa", "sfwerwer")])
由于某些原因,我总是会出现以下错误:

TypeError - cannot concatenate 'str' and 'long' objects
最后一行始终高亮显示

我不想连接任何东西。几乎不管我将“choices”参数的列表更改为什么,我都会得到这个错误


发生了什么事?

可能是
问题。id
是一个整数。试一试

self.fields['question_' + str(question.id)] = ...

相反。

'question.
是一个字符串,
question.id
是一个长字符串。您不能连接两个不同类型的内容,您必须使用
str(question.id)

将long转换为字符串。很可能,它只是高亮显示最后一行,因为您将语句拆分为多行

实际问题的解决方案很可能会改变

self.fields['question_' + question.id]

正如您可以在Python解释器中快速测试的那样,将字符串和数字一起添加是不起作用的:

>>> 'hi' + 6

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    'hi' + 6
TypeError: cannot concatenate 'str' and 'int' objects
>>> 'hi' + str(6)
'hi6'
>>“嗨”+6
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
“嗨”+6
TypeError:无法连接'str'和'int'对象
>>>“嗨”+str(6)
“hi6”
这看起来是个问题。试一试

"question_%f"%question.id


这是一个在一行中执行太多操作的问题-错误消息的帮助会稍微减少。如果你把它写在下面,那么这个问题会更容易找到

question_id = 'question_' + question.id
self.fields[question_id] = forms.ChoiceField(
                label=question.label,
                help_text=question.description,
                required=question.answer_set.required,
                choices=[("fe", "a feat"), ("faaa", "sfwerwer")])

请注意,“最后一行突出显示”,因为它指向错误所在的整个多行语句。非常感谢大家。这就解决了问题。
"question_%f"%question.id
"question_"+ str(question.id)
question_id = 'question_' + question.id
self.fields[question_id] = forms.ChoiceField(
                label=question.label,
                help_text=question.description,
                required=question.answer_set.required,
                choices=[("fe", "a feat"), ("faaa", "sfwerwer")])