我正在研究一个Django应用程序,该应用程序从API获取JSON数据并将其存储在PostgreSQL数据库中。但在迁移应用程序时,我收到此错误:
ValueError: invalid literal for int() with base 10: '262,400,000'
这里是追溯:
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 60, in handle
Projects.objects.create(**pdata)
File "/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/python/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/python/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1106, in execute_sql
for sql, params in self.as_sql():
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in as_sql
for obj in self.query.objs
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in <listcomp>
for obj in self.query.objs
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1058, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 998, in prepare_value
value = field.get_db_prep_save(value, connection=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: '262,400,000'
如何解决这个问题?
这是我的models.py代码:
from django.db import models
from django.contrib.postgres.fields import JSONField
from django.core.validators import URLValidator
class Projects(models.Model):
project_id=models.CharField(max_length=50)
projectfinancialtype=models.CharField(max_length=10)
regionname=models.CharField(max_length=50)
prodline=models.CharField(max_length=50)
lendinginstr=models.CharField(max_length=100)
lendinginstrtype=models.CharField(max_length=10)
envassesmentcategorycode=models.CharField(max_length=10)
supplementprojectflg=models.CharField(max_length=10)
productlinetype=models.CharField(max_length=10)
projectstatusdisplay=models.CharField(max_length=10)
status=models.CharField(max_length=10)
project_name=models.CharField(max_length=100)
board_approval_month=models.CharField(max_length=10)
approvalfy=models.CharField(max_length=9)
boardapprovaldate=models.DateTimeField(auto_now=False, auto_now_add=False)
closingdate=models.DateTimeField(auto_now=False, auto_now_add=False)
lendprojectcost=models.IntegerField()
ibrdcommamt=models.IntegerField()
idacommamt=models.IntegerField()
totalamt=models.IntegerField()
grantamt=models.IntegerField()
borrower=models.CharField(max_length=20)
impagency=models.CharField(max_length=20)
countryshortname=models.IntegerField()
prodlinetext=models.CharField(max_length=20)
project_abstract=JSONField()
p2a_flag=models.CharField(max_length=10)
p2a_updated_date=models.DateTimeField(auto_now=False, auto_now_add=False)
country_namecode=models.CharField(max_length=100)
projectinfo=models.TextField()
sector1=JSONField()
theme1=JSONField()
theme2=JSONField()
theme3=JSONField()
url=models.TextField(validators=[URLValidator()])
totalcommamt=models.IntegerField()
mjthemecode=models.CharField(max_length=20)
ccsa_practice_name=models.CharField(max_length=100)
combined_practice_code=models.CharField(max_length=100)
theme_namecode=JSONField()#ArrayField(JSONField(), blank=True)
themecode=models.IntegerField()
countrycode=models.CharField(max_length=10)
countryname=models.CharField(max_length=100)
docty=models.TextField()
mjtheme_namecode=JSONField() #ArrayField(JSONField(), blank=True)
source=models.CharField(max_length=10)
projectdocs=JSONField() #ArrayField(JSONField(), blank=True)
combined_practice_name=models.CharField(max_length=100)
mjtheme=JSONField() #ArrayField(JSONField(), blank=True)
ccsa_practice_code=models.CharField(max_length=100)
teamleaderupi=models.IntegerField()
locations=JSONField()
facets=JSONField()
这里是fetch.py文件的代码,它存储在/management/commands/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.get('projects')
for pdata in projects.values():
pdata['project_id'] = pdata.pop('id', None)
pdata['countryshortname'] = pdata.pop('countryshortname', None)
pdata['projectfinancialtype'] = pdata.pop('projectfinancialtype',None)
pdata['regionname'] = pdata.pop('regionname',None)
pdata['prodline'] = pdata.pop('prodline',None)
pdata['lendinginstr'] = pdata.pop('lendinginstr',None)
pdata['lendinginstrtype'] = pdata.pop('lendinginstrtype',None)
pdata['envassesmentcategorycode']=pdata.pop('envassesmentcategorycode',None)
pdata['supplementprojectflg']=pdata.pop('supplementprojectflg',None)
pdata['productlinetype']=pdata.pop('productlinetype',None)
pdata['projectstatusdisplay']=pdata.pop('projectstatusdisplay',None)
pdata['status']=pdata.pop('status',None)
pdata['project_name']=pdata.pop('project_name',None)
pdata['board_approval_month']=pdata.pop('board_approval_month',None)
pdata['approvalfy']=pdata.pop('approvalfy',None)
pdata['boardapprovaldate']=pdata.pop('boardapprovaldate',None)
pdata['closingdate']=pdata.pop('closingdate',None)
pdata['lendprojectcost']=pdata.pop('lendprojectcost',None)
pdata['ibrdcommamt']=pdata.pop('ibrdcommamt',None)
pdata['idacommamt']=pdata.pop('idacommamt',None)
pdata['totalamt']=pdata.pop('totalamt',None)
pdata['grantamt']=pdata.pop('grantamt',None)
pdata['teamleaderupi']=pdata.pop('teamleaderupi',None)
pdata['prodlinetext']=pdata.pop('prodlinetext',None)
pdata['project_abstract']=pdata.pop('project_abstract',None)
pdata['p2a_flag']=pdata.pop('p2a_flag',None)
pdata['p2a_updated_date']=pdata.pop('p2a_updated_date',None)
pdata['country_namecode']=pdata.pop('country_namecode',None)
pdata['projectinfo']=pdata.pop('projectinfo',None)
pdata['sector1']=pdata.pop('sector1',None)
pdata['theme1']=pdata.pop('theme1',None)
pdata['theme2']=pdata.pop('theme2',None)
pdata['theme3']=pdata.pop('theme3',None)
pdata['url']=pdata.pop('url',None)
pdata['totalcommamt']=pdata.pop('totalcommamt',None)
pdata['mjthemecode']=pdata.pop('mjthemecode',None)
pdata['theme_namecode']=pdata.pop('theme_namecode',None)
pdata['themecode']=pdata.pop('themecode',None)
pdata['countrycode']=pdata.pop('countrycode',None)
pdata['countryname']=pdata.pop('countryname',None)
pdata['docty']=pdata.pop('docty',None)
pdata['mjtheme_namecode']=pdata.pop('mjtheme_namecode',None)
pdata['source']=pdata.pop('source',None)
pdata['projectdocs']=pdata.pop('projectdocs',None)
pdata['mjtheme']=pdata.pop('mjtheme',None)
pdata['locations']=pdata.pop('locations',None)
pdata['facets']=pdata.pop('facets',None)
# other columns need to be ranamed
Projects.objects.create(**pdata)
这是我想将JSON响应存储到postgres数据库的API URL:API URL
答案 0 :(得分:1)
将逗号替换为_
(python 3支持下划线):
num.replace(',', '_')
稍后再更改。
答案 1 :(得分:1)
如果更改所有&#34;金额&#34;的数据库表数据类型到money那么你应该能够通过python将这些字段作为字符串插入并保留逗号。
俗话说,&#34;如果你有一把大而美丽的锤子......&#34;
所以,我使用sql函数将API data导入到数据库表中,然后这只是访问数据的方式之一:
http://dbfiddle.uk/?rdbms=postgres_10&fiddle=784a69df6106d5997875c798354a5777
以上指甲的一些有用链接:)
https://www.postgresql.org/docs/current/static/sql-createtype.html https://www.postgresql.org/docs/current/static/functions-json.html