3

I am working on a Django app and have created some initial tables in my db (SQLite3). I have JSON objects in a file (one JSON object per line) whose properties map one to one with the fields in the tables, I was wondering whether there was some in-built mechanism in Django to insert these into the tables, or do I have write the SQL myself?

At the moment my only reference is this

https://docs.djangoproject.com/en/1.7/howto/initial-data/

which suggests creating an <modelname>.sql file in a folder named sql inside the app directory with INSERT statements for the entries. But it seems there is no way of creating those INSERT statements from JSON objects. Writing a script is pretty simple, but I was wondering whether there was one already?

Also, at the moment in my app I have just one table, and I don't have any foreign key relationships. When I ran python manage.py makemigrations <app> it did not create any indexes for the primary key field in the table. Is this something I have to do myself?

Thanks for any help.

2
  • Are you looking at the same link? That has an actual example of a JSON file being used as data fixture to populate a database. Feb 24, 2015 at 22:38
  • I did try that (I ran python manage.py loaddata <appname>/fixtures/<filename>.json) but I get an error - the last line in the traceback is /python.py", line 93, in Deserializer Model = _get_model(d["model"]) django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/srm/Documents/sandeep/cst/dev/TPP/TPP/TPP_App/fixtures/SmallGroupsData_Order1-35.json': u'model'
    – user997225
    Feb 24, 2015 at 22:47

1 Answer 1

0

I'm assuming that the table is associated with some data model in your models.py file (if not then you need to write INSERT statements explicitly). Once you link your table with the data model(using python manage.py syncdb), just load the data from JSON into an instance of your model using json.loads() function(import json). Then save using the 'instance_you_created.save()' method. That should work i guess.

3
  • The model for the table is defined in models.py and the table was created in the db using migrations. I am using Django 1.7+, isn't syncdb replaced with something else? When you say an instance of the model, is that via python manage.py shell to interact with the app?
    – user997225
    Feb 25, 2015 at 18:16
  • by instance i mean the instance your need to create in your views.py file or wherever you are getting the request from your front-end, meaning the request having the JSON object.
    – Paritosh
    Feb 26, 2015 at 10:33
  • I have a custom parser that reads from a JSON file (all my data is in several JSON files, each one contains about 1000 JSON objects, each object on a separate line) and loads an ordered dictionary of JSON objects. This parser is a class that is in the static subdirectory of the app folder. Using python manage.py shell and import django; django.setup() I should be able to load this class in the interpreter and use its methods to load the JSON data and save it to the app db tables. But I cannot seem to do this.
    – user997225
    Mar 2, 2015 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.