Reputation: 1164
I'm using Django (written a script here). I'm trying to parse a JSON file and add it to a model I've created . However, I'm getting this weird error that I'm unable to figure out !
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "json_api.settings"
import json
from api.models import User_details
with open('./static/test.json') as data_file:
data = json.load(data_file)
r = User_details.objects.create(uid=int(data["User"]["id"]) ,
premium=bool(data["User"]["premium"]) ,
last_filled=data["LastFilled"] ,
trip_mode=data["Trip_Mode"] ,
odometer=int(data["Params"]["Odometer"]),
fuel=int(data["Params"]["Fuel"]),
tirep_fl=int(data["Params"]["Fuel"]["frontLeft"]),
tirep_fr=int(data["Params"]["TireP"]["frontRight"]),
tirep_bl=int(data["Params"]["TireP"]["backLeft"]),
tirep_br=int(data["Params"]["TireP"]["backRight"]))
r.save()
JSON File :
{
"User": {
"id": "123456678923",
"premium": "False"
},
"LastFilled": "16/02/02-00:45:32",
"Trip_Mode": "Disabled",
"Params": {
"Odometer": "12345",
"Fuel": "0.78",
"TireP": {
"frontLeft": "32",
"frontRight": "29",
"backLeft": "24",
"backRight": "26"
}
}
}
Error :
(env) saru95 $ python import_data.py
File "import_data.py", line 19
r.save()
^
SyntaxError: invalid syntax
EDIT :
After adding the missing parenthesis and running python import_data.py
i get the following error :
(env) saru95 $ python import_data.py
Traceback (most recent call last):
File "import_data.py", line 4, in <module>
from api.models import User_details
File "/Users/sarthakmunshi/Desktop/django-cookbook/json_api/api/models.py", line 3, in <module>
class User_details(models.Model):
File "/Users/sarthakmunshi/Desktop/django-cookbook/env/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/sarthakmunshi/Desktop/django-cookbook/env/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/Users/sarthakmunshi/Desktop/django-cookbook/env/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Upvotes: 0
Views: 108
Reputation: 1164
I solved the problem by replacing
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "json_api.settings"
by
from json_api.wsgi import *
where json_api
is my project name . Thanks for the help !
Upvotes: 0
Reputation: 44838
You forgot lots of closing parentheses:
fuel=int(data["Params"]["Fuel"]),
tirep_fl=int(data["Params"]["Fuel"]["frontLeft"]),
tirep_fr=int(data["Params"]["TireP"]["frontRight"]),
tirep_bl=int(data["Params"]["TireP"]["backLeft"]),
tirep_br=int(data["Params"]["TireP"]["backRight"])
Upvotes: 0
Reputation: 599610
You're missing close parens for the int
calls from fuel
and all four of the tirep_*
values.
Upvotes: 1