Django 测试数据没有有效的模型标识符?
我相信这里有个简单的答案,但我就是看不出来。我想把一些数据加载到我的数据库里,但无论我用哪个模型标识符,都会出现 DeserializationError: invalid model identifier:...
这个错误。
文件结构:
testproject/
testapp/
fixtures/
data.json
__init__.py
models.py
tests.py
views.py
sqlite3.db
__init__.py
manage.py
settings.py
urls.py
因为这是我第一次使用数据加载,所以我参考了 http://www.djangoproject.com/documentation/models/fixtures/ 上的模型:
from django.db import models
from django.conf import settings
class Article(models.Model):
headline = models.CharField(max_length=100, default='Default headline')
pub_date = models.DateTimeField()
def __unicode__(self):
return self.headline
class Meta:
ordering = ('-pub_date', 'headline')
data.json:
[
{
"pk": "3",
"model": "testapp.article",
"fields":
{
"headline": "Time to reform copyright",
"pub_date": "2006-06-16 13:00:00"
}
},
{
"pk": "2",
"model": "testapp.article",
"fields":
{
"headline": "Poker has no place on ESPN",
"pub_date": "2006-06-16 12:00:00"
}
},
{
"pk": "1",
"model": "testapp.article",
"fields":
{
"headline": "Python program becomes self aware",
"pub_date": "2006-06-16 11:00:00"
}
}
]
我试过 testapp.article
、testproject.article
和 testproject.testapp.article
,结果都出现了同样的错误。我使用的是 1.2.4 版本,Python 2.6,并且用的是 loaddata,而不是 syncdb。有没有什么想法?
4 个回答
我不确定这是否有帮助,但我现在正在查看我写的一些数据文件,发现我的模型标识符都写得很规范。
这里有一个来自我用户账户数据文件的例子,不过请注意,它是用YAML格式写的。
- model: auth.User
pk: 4
fields:
username: avirtue
first_name: Aurora
last_name: Virtue
is_active: true
is_superuser: false
is_staff: false
password: sha1$90431$9347d343e94122f94f9f02988f026a76d339ab02
email: avirtue@someschool.edu
- model: users.UserProfile
pk: 4
fields:
user: 4
school_id: 420985
professor: false
这个文件放在users/fixtures/文件夹下(也就是说,有一个应用叫做users,这个文件就在这个应用的fixtures文件夹里)。
正如你所看到的,这些模型实际上来自两个不同的地方。我使用的第二个模型来自同一个应用,定义了一个UserProfile
。第一个模型实际上来自django.contrib.auth
模块,这是项目用来进行用户认证的。
试着检查一下settings.py文件。在我的情况下,我只是忘了把应用程序添加到INSTALLED_APPS里。
你的 data.json 文件没问题,我试过了,能正常工作。
你确定你的数据库和模型是同步的吗?
你是用什么命令来加载这个文件的?
正如 Luc 建议的,比较一下 "manage.py dumpdata testapp" 的输出和你的文件。