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?

ian-campbell
  • 1,605
  • 1
  • 20
  • 42
  • Are you talking about Django or Django Rest Framework? – jdotjdot Nov 26 '14 at 07: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 – ian-campbell Nov 26 '14 at 09:08
  • this can help you: http://stackoverflow.com/questions/9686409/how-to-store-a-dictionary-in-a-django-database-models-field – doniyor Nov 26 '14 at 09:24

2 Answers2

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()
wolendranh
  • 4,202
  • 1
  • 28
  • 37
  • 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. – ian-campbell Nov 27 '14 at 05:03
  • @edmund_spenser Are you talkink about json file like some initial data from which you will ceate a DB? Without created models in app? – wolendranh Nov 27 '14 at 09:22
  • That's right, I don't even need the models really. I want to set up a database and then use it with Django. – ian-campbell Nov 28 '14 at 04:23
  • 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 - https://docs.djangoproject.com/en/1.7/howto/initial-data/ – wolendranh Nov 28 '14 at 06:21
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.

ian-campbell
  • 1,605
  • 1
  • 20
  • 42