已自行解决,代码如下:
json格式:
[
{
"标题": "小武",
"内容": "测试",
"类型":["情感","文学","散文"]
"文章资源":[
{
"title":"小武.1998.HD.1080p.x264.aac.Mandarine .CHS.mkv ",
"links":"https://www.xxx1.com"
},
{
"title":"小武.1998.HD.1080p.x264.aac.Mandarin.CHS.mp4",
"links":"https://www.xxx2.com"
}
]
},
{
...
}
]
from (自己项目名称).wsgi import *
from (app名称).models import Types,DetailInfo,MagnetLink
import json
def insertDB():
with open('E:\workspace\xxxxx.json', 'r', encoding='utf8') as f:
data = json.load(f)
for list in data:
#先写入分类(多对多关系)
for t in list['类型']:
c = Types.objects.get_or_create(name=t)[0]
# 然后插入文章
article = DetailInfo.objects.get_or_create(title=list['标题'],
context=list['内容'])[0]
# 最后获取文章id
article_id = article.id
for m in list['文章资源']:
print(m)
# 根据id插入数据到文章资源表中(一对多)
ResourcesLink.objects.get_or_create(detailinfo_id=article_id,title=m['title'],link=m['links'])[0]
article.types.add(c)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…