ValueError:以10为基数的int()的文本无效:'';[Django]

ValueError:以10为基数的int()的文本无效:'';[Django],django,python-3.x,api,django-models,valueerror,Django,Python 3.x,Api,Django Models,Valueerror,我正在开发一个Django应用程序,它从API获取JSON数据并将其存储在PostgreSQL数据库中。 但在迁移应用程序时,我遇到以下错误: ValueError: invalid literal for int() with base 10: '' 我应该在代码中更改什么来解决此错误? 以下是回溯: Traceback (most recent call last): File "manage.py", line 22, in <module> execute_fr

我正在开发一个Django应用程序,它从API获取JSON数据并将其存储在PostgreSQL数据库中。 但在迁移应用程序时,我遇到以下错误:

ValueError: invalid literal for int() with base 10: ''
我应该在代码中更改什么来解决此错误? 以下是回溯:

Traceback (most recent call last):


File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 87, in database_forwards
    field,
  File "/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 415, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 155, in column_sql
    default_value = self.effective_default(field)
  File "/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 229, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 762, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1853, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''
下面是fetch.py文件的代码,该文件存储在/management/commands/fetch.py下:

import requests
from django.core.management.base import BaseCommand
from worldBank.worldBankApp.models import Projects

class Command(BaseCommand):
    def handle(self, **options):
        response = requests.get("https://search.worldbank.org/api/v2/projects?format=json&countryshortname_exact=India&source=IBRD&kw=N&rows=7")
        data = response.json()
        projects = data['projects']

        #print(data)

        for project in projects:
            print(projects[project])
            print("\n\n")

            data = projects[project]

            Projects.objects.create(

                project_id = data['id'],
                project_name = data['project_name'],
                status = data['status'],
                country = data['countryshortname'],
                locations = data['locations'],
                mjtheme = data['mjtheme'],
                project_docs = data['projectdocs'],
                source = data['source'],
                mjtheme_namecode = data['mjtheme_namecode'],
                docty = data['docty'],
                countryname = data['countryname'],
                countrycode = data['countrycode'],
                themecode = data['themecode'],
                theme_namecode = data['theme_namecode'],
                project_url = data['url'],
                totalcommamt = data['totalcommamt'],
                mjthemecode = data['mjthemecode'],
                sector1 = data['sector1'],
                theme1 = data['theme1'],
                theme2 = data['theme2'],
                theme3 = data['theme3'],
                projectinfo = data['projectinfo'],
                country_namecode = ['country_namecode'],
                p2a_updated_date = data['p2a_updated_date'],
                p2a_flag = data['p2a_flag'],
                project_abstract = data['project_abstract']

                )

以下任一
整型字段出现错误

themecode=models.IntegerField()
theme_namecode=models.IntegerField()
totalcommamt=models.IntegerField()
mjthemecode=models.IntegerField()
p2a_updated_date=models.IntegerField()
原因:您应该分配整数值(例如4或“4”),而不是它的值。请检查以上字段的值

======编辑

响应整数字段的值:

==编辑:将以下代码放在
data=projects[project]

for k, v in data.items():
    try:
        int(v)
    except ValueError:
        print k, v
        break

以下任一
整型字段出现错误

themecode=models.IntegerField()
theme_namecode=models.IntegerField()
totalcommamt=models.IntegerField()
mjthemecode=models.IntegerField()
p2a_updated_date=models.IntegerField()
原因:您应该分配整数值(例如4或“4”),而不是它的值。请检查以上字段的值

======编辑

响应整数字段的值:

==编辑:将以下代码放在
data=projects[project]

for k, v in data.items():
    try:
        int(v)
    except ValueError:
        print k, v
        break

Navyad在很大程度上是正确的,您正在尝试将字符串分配给整数字段(
themecode
就是一个很好的例子)


通过JSON,我看到了类似于
“themecode”:“79,77,66”
。这不能简单地插入到整数字段中。您需要将这些更改为
模型。CharField
或以某种方式预处理数据(例如提取第一个数字)。

Navyad基本正确,您正在尝试将字符串分配给整数字段(
themecode
是一个很好的示例)


通过JSON,我看到了类似于
“themecode”:“79,77,66”
。这不能简单地插入到整数字段中。您需要将这些更改为
models.CharField
或以某种方式预处理数据(例如提取第一个数字)。

我是否应该包括IntegerField(max_length=100)?但这并不能解决问题否。问题是当您分配像'p2a\u updated\u date=data['p2a\u updated\u date']这样的值时。这里的数据['p2a\u updated\u date']应该是整数。尝试分配IntegerField('100'),但同样的问题确定让我试试'p2a\u updated\u date=int(data['p2a\u updated\u date']),那么哪个字段适合这些值@navyad请帮助我是否应该包括IntegerField(max_length=100)??但没有解决问题。问题是当您分配像'p2a\u updated\u date=data['p2a\u updated\u date']这样的值时。这里的数据['p2a\u updated\u date']应该是整数。尝试分配IntegerField('100'),但同样的问题确定让我试试'p2a\u updated\u date=int(data['p2a\u updated\u date']),那么哪个字段适合这些值@navyad please Help我将所有IntegerField()更改为CharField(max_length=100),但仍然存在相同的错误:ValueError:以10为基数的int()的文本无效:“”甚至尝试将所有IntegerField()更改为CharField(),但得到相同的错误我将所有IntegerField()更改为CharField(max_length=100)但仍然存在相同的错误:ValueError:以10为基数的int()的文本无效:“”甚至尝试将所有IntegerField()更改为CharField(),但得到相同的错误