2017-11-17 4 views
0

APIからJSONデータを取り出してPostgreSQLデータベースに格納するDjangoアプリケーションを開発中です。 しかし、アプリを移行しながら、私はこのエラーを取得しています:ValueError:基数10のint()のリテラルが不正です '' [Django]

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: '' 

どのようにこの問題を解決するには: ここにトレースバックですか?ここで

はmodels.pyのための私のコードです:

from django.db import models 
from django.contrib.postgres.fields import JSONField 

class Projects(models.Model): 
     data = JSONField(null=False) 
     project_id=models.CharField(max_length=100) 
     project_name=models.CharField(max_length=100) 
     status=models.CharField(max_length=10) 
     country=models.CharField(max_length=100) 
     locations=JSONField() 
     mjtheme=models.CharField(max_length=50) 
     project_docs=JSONField() 
     source=models.CharField(max_length=10) 
     mjtheme_namecode=models.CharField(max_length=10) 
     docty=models.TextField() 
     countryname=models.CharField(max_length=100) 
     countrycode=models.CharField(max_length=10) 
     themecode=models.IntegerField() 
     theme_namecode=models.IntegerField() 
     project_url=models.TextField() 
     totalcommamt=models.IntegerField() 
     mjthemecode=models.IntegerField() 
     sector1=models.CharField(max_length=10) 
     theme1=models.CharField(max_length=10) 
     theme2=models.CharField(max_length=10) 
     theme3=models.CharField(max_length=10) 
     projectinfo=models.TextField() 
     country_namecode=models.CharField(max_length=5) 
     p2a_updated_date=models.IntegerField() 
     p2a_flag=models.CharField(max_length=5) 
     project_abstract=JSONField() 

そして、ここでは/management/commands/fetch.py​​の下に格納され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'] 

       ) 

答えて

1

エラーがあります次のいずれかに来るIntegerField

themecode=models.IntegerField() 
theme_namecode=models.IntegerField() 
totalcommamt=models.IntegerField() 
mjthemecode=models.IntegerField() 
p2a_updated_date=models.IntegerField() 

理由:代わりに整数値(4または '4'など)を割り当てます。上記のフィールドの値を確認してください。

===== EDIT

整数フィールド(複数可)に対する応答に来る値:

themecode: "79,77,66", # invalid 
mjthemecode: "10,10,5", # invalid 
p2a_updated_date: "2017-09-01 00:00:00.0", # invalid 
theme_namecode: [] # invalid 

==== EDIT:置き、次のコード

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

IntegerField(max_length = 100)を含める必要がありますか?問題は解決しません。 – 4M01

+0

問題は、 'p2a_updated_date = data ['p2a_updated_date']'のような値を割り当てるときです。ここで、data ['p2a_updated_date']は整数でなければなりません。 – navyad

+0

IntegerField( '100')でも同じ問題を割り当てようとしました。 – 4M01

0

data = projects[project]後Navyadは大体正しいです、あなたは整数フィールドに文字列を割り当てようとしています(themecodeが良い例です)。

JSONを見て、私は"themecode": "79,77,66"のようなものを見ています。これは単純には整数フィールドにを挿入できません。それらをmodels.CharFieldに変更するか、何らかの形でデータを前処理する必要があります(たとえば、最初の数字を抽出します)。

+0

すべてのIntegerField()をCharField(max_length = 100)に変更しましたが、同じエラー:ValueError:基数10のint()のリテラルが無効です: '' – 4M01

+0

すべてのIntegerField()をCharField()エラー – 4M01

関連する問題