python:无法连接“str”和“long”对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3532873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:36:16  来源:igfitidea点击:

python: cannot concatenate 'str' and 'long' objects

python

提问by Roger

I'm trying to set up a choice field in django, but I don't think this is a django issue. The choices field takes an iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field.

我正在尝试在 django 中设置一个选择字段,但我认为这不是 django 问题。选择字段采用 2 元组的可迭代(例如,列表或元组)作为该字段的选择。

Here's my code:

这是我的代码:

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")])

for some reason, i always get the following error:

出于某种原因,我总是收到以下错误:

TypeError - cannot concatenate 'str' and 'long' objects

The last line is always highlighted.

最后一行始终突出显示。

I'm not trying to concatenate anything. Almost regardless of what I change the list to for the 'choices' parameter, I get this error.

我不想连接任何东西。几乎无论我将“选择”参数的列表更改为什么,我都会收到此错误。

What's going on?

这是怎么回事?

采纳答案by Mark Rushakoff

Most likely it's highlighting the last line only because you split the statement over multiple lines.

很可能它突出显示最后一行只是因为您将语句拆分为多行。

The fix for the actualproblem will most likely be changing

实际问题的修复很可能会发生变化

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

to

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

As you can quickly test in a Python interpreter, adding a string and a number together doesn't work:

由于您可以在 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'

回答by kennytm

Probably question.idis an integer. Try

大概question.id是一个整数。尝试

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

instead.

反而。

回答by Sjoerd

'question_'is a string, question.idis a long. You can not concatenate two things of different types, you will have to convert the long to a string using str(question.id).

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

回答by Umang

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

That looks like the problem. Try

这看起来像问题。尝试

"question_%f"%question.id

or

或者

"question_"+ str(question.id)

回答by John La Rooy

This is a problem with doing too many things in one line - the error messages become slightly less helpful. Had you written it as below the problem would be much easier to find

这是在一行中做太多事情的问题 - 错误消息变得不太有用。如果你把它写成下面这个问题会更容易找到

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")])