我有一些JSON格式的数据,我想用manage.py loaddata data.json
命令加载这些数据。我有JSON格式。
{
"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"
},
这里我的问题是如何为VideoData定义模型字段?视频内部的对象可能会有所不同。
答案 0 :(得分:0)
您的模型应包含两个通过foreignkey关系链接的表来存储数据,如下所示:
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')
要从JSON实际加载类似于您提供的模型,您可能需要natural keys