Monday, March 2, 2015

Re: Populating Django app db with JSON data

Hello,

Indeed, the data migration is the best way. Check out
the documentation here:
https://docs.djangoproject.com/en/1.7/ref/migration-operations/#django.db.migrations.operations.RunPython

You write your function that will be called by the RunPython
and will load your JSON.
Migration are ordered, your first migration will create the
tables and the second (your data migration) will load your
JSON.

To create an empty migration:
./manage.py makemigrations <app> --empty

You can rename to a useful descriptive name the migration
file.

Have a good one


On 03/02/2015 08:16 AM, Sandeep Murthy wrote:
Hi

I've tried to get the answer to this question (which is a bit open-ended) on stackoverflow without much success, which
is basically this: what is the recommended approach to populating a pre-existing Django app database table (generated
from a model and which is currently empty) with JSON data?

There seem to be several alternatives given in the Django documentation (Django 1.7 manual) which include (1) fixtures,
(2) SQL scripts, (3) data migrations.  Of these I am a bit confused by the advice in the manual which suggests that (1)
and (2) are only useful for loading initial data.  That's not what I want to do.  The data that the app needs is going to be
persistent and permanent because the app is intended to be a web query tool for a large dataset that is currently in the
form of several JSON files, each containing on average thousands of JSON objects, each object representing an entry
corresponding to a table entry in a relational db.  The data is not going to be re-loaded or change after entry, and there
is no user facility for changing the data.

The table has been created using the makemigrations and migrate tools, but is empty.  I just need to populate the
table with the JSON data.  It seems that I need to write a custom data migration script that will insert the data into the
table via the interpreter, and then I need to run python manage.py migrate.  Is this the case, and if so, are there
are examples that I could use?

Thanks in advance for any suggestions.

SM
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/db5919c5-ace4-4556-b90e-aa47baa26552%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment