为了学习如何在数据库中导入初始数据我创建了模型,
from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
之后,我使用.json格式的灯具,如下所示,
[ { "model": "myapp.person", "pk": 1, "fields": { "first_name": "John", "last_name": "Lennon" } }, { "model": "myapp.person", "pk": 2, "fields": { "first_name": "Paul", "last_name": "McCartney" } } ]
它会在loaddata上抛出错误
File "C:\Python27\lib\site-packages\django\core\serializers\python.py", line 96, in Deserializer Model = _get_model(d["model"]) django.core.serializers.base.DeserializationError: Problem installing fixture 'I:\DJANGO\library\myapp\fixtures \bookdata.json': string indices must be integers
但是当我使用YAML格式的夹具时,如下所示,
- model: myapp.person pk: 1 fields: first_name: John last_name: Lennon - model: myapp.person pk: 2 fields: first_name: Paul last_name: McCartney
它就像一个魅力.
现在我很困惑,因为整件事只是从他们的文件中复制出来.我使用的是Windows 32bit,Django 1.9,python 2.7.