django value eRror无效的int()base 10:''如何解决无类型错误
以django表单添加值时,从old_car字段中获取 valueerror无效的int()to in int()base 10:''
new_car和old_car的值是字符串,需要转换为Integer,以进一步条件,我知道如果我删除int将解决错误:)。
我如何解决该
django forms.py
car = cleaned_data.get("user", ['AUTO'])
if car[0] == 'AUTO':
records = []
for i in range(count):
new_car = int(self.data.get(f'cardefination_set-{i}-new_car', []))
old_car =int(self.data.get(f'cardefination_set-{i}-old_car', []))
records.append((new_car, old_car))
records = sorted(records, key=lambda record: record[1])
Trackback
https://stackoverflow.com/posts/72548081/edit#
in Django forms when i am appending the values , from old_car field getting ValueError invalid literal for int() with base 10: ''
the values of new_car and old_car are string and need to convert to integer for further conditions, i know if i remove int will solve the error :).
how i can solve that
django forms.py
car = cleaned_data.get("user", ['AUTO'])
if car[0] == 'AUTO':
records = []
for i in range(count):
new_car = int(self.data.get(f'cardefination_set-{i}-new_car', []))
old_car =int(self.data.get(f'cardefination_set-{i}-old_car', []))
records.append((new_car, old_car))
records = sorted(records, key=lambda record: record[1])
traceback
https://stackoverflow.com/posts/72548081/edit#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不了解您的代码的情况下,我将解决与这些行相关的几个问题:
这两行中的问题都是相同的。您是
casting
(显式类型转换)使用int
使用int(value)
。这意味着该值必须“转换”到int
。在您的情况下,您将从self.data
中获得一个值,我将假设它是字典。如果无法将获得的值转换为int
,您将获得该错误。此外,我看到您在
dict.get(键,值)
函数上使用默认值参数。您提供的默认值是
[]
是list
, ieint(int([])
将投掷:该错误不是您提到的错误,而只是想指出这一点。
我认为您的问题是您从字典中获得的价值是一个空字符串。
因此,您的错误:
确保您从dict获得的东西可以转换为
int
Without knowing more about your code I will address a couple of issues related with these lines:
The issue is the same in both lines. You are
casting
(explicit type conversion) a value toint
usingint(value)
. This means that the value must be "convertible" toint
. In your case you are getting a value fromself.data
, that I am going to assume it is a dictionary. If the obtained value can't be converted toint
you will get that error.Moreover, I see you are using the default value parameter on the
dict.get(key, value)
function.The default value you are providing is
[]
which is alist
, i.e.int([])
will throw:That error is not the error you are mentioning, but just wanted to point that out.
I think your issue is that the value you are getting from the dictionary is an empty string.
Hence your error:
Make sure what you are getting from the dict can be converted to
int