0

I'm trying to create a database in Django with data stored in json.

 {
   'Vincent van Gogh': 
       {'The Starry Night': ['starryLink'], 
        'Irises': ['irisesLink']}, 
   'Leonardo da Vinci': 
       {'Mona Lisa': ['monalisalink'], 
        'The Last Supper': ['lastsupperlink']},
 }

Do I need to rewrite the above json into the format below?

[
{
    "model": "model.artistModel",
    "pk": 1,
    "fields": {
        "artist": "Vincent van Gogh",
        "title": "The Starry Night",
        "link": ["link.com"]
    }
},

I saw some examples of creating a Django db from a json but none looked like my json file. Do I have to script my data into a db with loops and such or can Django do it automatically?

3
  • Are you talking about Django or Django Rest Framework? Commented Nov 26, 2014 at 7:04
  • Just the basic Django app. I haven't learned what Django Rest is yet. I'm just trying to create a new Django database with data stored in json Commented Nov 26, 2014 at 9:08
  • this can help you: stackoverflow.com/questions/9686409/… Commented Nov 26, 2014 at 9:24

2 Answers 2

1

There is package Django Json Field. It has support of Python 3 and Django 1.7. Example:

from django.db import models
from jsonfield import JSONField

class MyModel(models.Model):
  json = JSONField()
Sign up to request clarification or add additional context in comments.

4 Comments

I don't need to keep my json file intact. I just want to take the data and make a mysql database out of it for queries.
@edmund_spenser Are you talkink about json file like some initial data from which you will ceate a DB? Without created models in app?
That's right, I don't even need the models really. I want to set up a database and then use it with Django.
I think you will still need to create models fro data that you want to loadinto db. For example model - Painter for your data from example. And then use initial.json like in this tutorial - docs.djangoproject.com/en/1.7/howto/initial-data
1

To answer my own question, I had to rewrite my json file to be compliant with the Django model. I created my model in models.py to look like

class Art(models.Model):
    title = models.CharField()
    artist = models.CharField()
    link = models.URLField()

    def __unicode__(self):
        return u'%s, %s, %s' % (self.title, self.artist, self.link)

I went to the /admin/ panel, clicked on the Art model, and put in data and saved it. Then I ran

python manage.py dumpdata mydata.json

to see what the database would put out. The json it put out was in this format

[
{
     "pk": 1,  
     "model": "model.artistModel",
     "fields": {
         "artist": "Vincent van Gogh",
         "title": "The Starry Night",
         "link": "link.com"
     }
},

So I rearranged my json file to have that same structure, dropped in in my django /fixtures folder then ran

python manage.py loaddata myfixture.json

and like magic Django populated the database with all my data.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.