2017-11-18 13 views
2

APIからJSONデータを取得してPostgreSQLデータベースに格納するDjangoアプリケーションで作業しています。しかし、私はこのエラーを取得していますアプリの移行中:KeyError:Djangoアプリの「場所」

KeyError: 'locations' 

はここでトレースバックです:

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 "/app/aggregator/WorldBank/management/commands/fetch_wb.py", line 23, in handle 
    locations = data['locations'], 
KeyError: 'locations' 

この問題を修正する方法は?ここで

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

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

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

そして、ここでは/management/commands/fetch.py​​の下に格納されfetch.py​​ファイルのコードです:

import requests 
from django.core.management.base import BaseCommand 
from aggregator.WorldBank.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=776") 
     data = response.json() 
     projects = data['projects'] 

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

       ) 

ですJSONレスポンスをpostgresデータベースに保存するAPI URL: API URL

私はmodels.pyを効率的に定義して保存することができますこのJSONのすべてのフィールドがデータベースに応答しますか?

+0

、何の 'locations'キーがありません。 –

答えて

1

あなたのトラブルが再それを試してdataを宣言している:明らか

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

    projects = data.get('projects') 

    for pdata in projects.values(): 
     pdata['project_id'] = pdata.pop('id', None) 
     pdata['country'] = pdata.pop('countryshortname', None) 
     # other columns need to be ranamed 
     Projects.objects.create(**pdata) 
+0

答えをお寄せいただきありがとうございます。しかし、このコードはどこに書きますか? models.pyまたはfetch.py​​? – 4M01

+0

編集して助かりました –

+0

ありがとうございました! – 4M01

関連する問題