- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Django 应用程序,它从 API 获取 JSON 数据并将其存储在 PostgreSQL 数据库中。但是在迁移应用程序时出现此错误:
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 响应中的所有字段存储到数据库中?
最佳答案
你的麻烦在于重新声明 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)
关于python - key 错误 : 'locations' in Django app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47364215/
我正在尝试在 python 中编写正则表达式来查找目录路径:我的文本如下所示: text = "The public disk is: \\\\diskA\\FolderB\\SubFolderC\\
我想写一个LocationListener,它把最近最精确的位置作为它的位置。我打算在我的 LocationListener 中使用此代码: @Override public void
我想建立一个有光泽和 plotly 的交互式图表。 Shiny 有一个内置功能来获取有关用户交互的信息。比如:input$plot_click、input$plot_dblclick、input$pl
我正在使用 MobileFirst 提供的 WL.Device.Geo.acquirePosition(onGeoLocationSuccess, onGeoLocationFailure, opti
我最近开始使用 ionic 框架,它里面有 angular js。为了在屏幕之间导航,我使用了 $location.path 并且效果很好。但是,在我下载的一个示例中,我看到 $state.go 被用
谁能解释一下这种行为?我使用 history.pushState(null, null, '#test'); 推送历史状态。然后,当尝试使用console.log(window.location.ha
这里是相关代码: https://www.facebook.com/sharer/sharer.php?u={{$location.absUrl()}} https://www.facebook.c
这两个重定向之间有什么区别?我有一个应用程序,当我使用时,它可以在 chrome 和 android 4 上正常工作,但在 android 2.x.x 上不能正常工作 document.locatio
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
有什么区别 window.location.href="http://example.com"; window.location.replace("http://example.com"); wind
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
以下 3 个指令之间有区别吗? location ~* \.(png)$ { expires max; log_not_found off; } location ~ \.(png)$ {
位于正文末尾之前的以下脚本在 Internet Explorer 和 Chrome(以及任何其他浏览器)中都会被调用。但重定向到指定的 URL 仅发生在 IE 中。我还尝试了 window.locat
我正在使用 Angular ngRouter。我需要更改 url 路径以及搜索参数。我知道 $location.path 和 $location.search,但是有没有一个函数可以同时设置它们? 最
在angularjs中用$location和window.location哪个更好。 例如,我们可以使用$location.path() 或window.location.href 来完成同样的工作。
我在我的网站上使用上述 2 个命令。似乎它们对 95% 访问它应该触发的页面的人有效,但对其他人则不会。 有谁知道是否可以完全阻止这些 javascript 命令?我真的很头疼为什么它们有时不起作用。
这是我无法弄清楚的另一个错误。 我有这个类ExtendedLocation extends Location实例化时抛出 ClassCastExceptioncurrentGpsLocation =
我一直在尝试简单地将一个包含两个变量(一个字符串和一个位置)的类推送和读取到 firebase,但我一直收到此错误。 **com.google.firebase.database.DatabaseEx
我注意到 iPhone 上的“常用位置”似乎比监控 iOS 访问的应用程序 (https://developer.apple.com/reference/corelocation/clvisit) 使
在我的 javascript 代码中,在某些时候,我需要刷新窗口(用户上传了新图片但在页面中仍然可以看到它) 我想知道为什么 location.href = location.href 不刷新窗口?
我是一名优秀的程序员,十分优秀!