0

I Have some data in JSON format and i want to load that data with manage.py loaddata data.json command. I have below JSON format.

{  
  "fields":{  
     "user id":12026,
     "user name":"Paul Graham",
     "email":"pgraham0@sun.com",
     "city":"China",
     "VIdeoData":[  
        {  
           "video":"Livetube",
           "time":0
        },
        {  
           "video":"Leexo",
           "time":22
        }
     ]
  },
  "pk":1,
  "model":"graph.videometadata"
},

Here my question is how will i define model fields for VideoData? Object inside the videodata could vary.

Naresh
  • 1,842
  • 2
  • 24
  • 36
  • try this approach http://stackoverflow.com/a/5726226/3033586 – madzohan Jul 10 '15 at 07:31
  • That depends on *how* the videodata will vary. Unlike in a NoSQL database a Django model needs to know exactly what can and can not be stored in the model. You may need polymorphism and a custom parser to actually load the data... – EvertW Jul 10 '15 at 07:32
  • @EvertW in above example videodata have two object ...... but it could be 3 objects inside it or 10 but rows inside object is constant i.e every object will have video and time field....any idea how can i achieve this... – Naresh Jul 10 '15 at 07:39

1 Answers1

0

Your model should contain two tables linked through a foreignkey relationship to store the data, like so:

from django.db.models import Model
from django.db import models

class User(Model):
    user_id = models.IntegerField()
    user_name = models.CharField(max_length=40)
    email = models.EmailField()
    city = models.CharField(max_length=40)

class VideoData(Model):
    video = models.CharField(max_length=40)
    time  = models.IntegerField()
    user = models.ForeignKey(User, related_name='VideoData')

To actually load the model from JSON similar to what you provided, you may need natural keys

Community
  • 1
  • 1
EvertW
  • 1,160
  • 9
  • 18
  • i defined model as per your solution but when i used "manage.py loaddata" command i got error : "User has no field named u'VIdeoData'"...please suggest me the solution.. – Naresh Jul 10 '15 at 08:38
  • You might try adding a related_name to the foreign key, I'll update the model. Haven't tried loading JSON though. – EvertW Jul 11 '15 at 07:00
  • Now i am getting error : " VideoData has no field named u'user_name'" – Naresh Jul 11 '15 at 12:08