我试图创建一些装置来在Django中运行测试。现在,我只是从我的开发数据库转储适当的模型,然后通过测试加载这些模型。下面是我用转储灯具的命令:当在Django装置中加载auth.user和配置文件模型时出现IntegrityError
python manage.py dumpdata accounts.Profile auth.User -n auth.User --indent 4 -e contenttypes > path/to/fixture.json
继this question和this one使用自然键和排除的内容类型我已经添加标志。这并没有帮助 - 我收到此错误信息:
IntegrityError: Could not load accounts.Profile(pk=1): duplicate key value violates unique constraint "accounts_profile_user_id_key"
DETAIL: Key (user_id)=(1) already exists
我已经手动检查夹具和只有一个针对该用户ID条目。个人资料模型非常标准,还有一些额外的个人信息字段。它连接到用户的模型如下:
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
在完整的名字,这里的灯具的样子:
{
"pk": 1,
"model": "auth.user",
"fields": {
"username": "unique_username",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2013-03-31T23:19:44.391",
"groups": [],
"user_permissions": [],
"password": "secret",
"email": "email",
"date_joined": "2013-03-13T21:30:39.225"
}
},
{
"pk": 1,
"model": "accounts.profile",
"fields": {
"status": "active",
"first_name": "John",
"last_name": "Smith",
"middle_name": null,
"headline": "Something very cool",
"user": [
"unique_username"
],
"location": null
}
}
任何想法?是因为我使用链接用户和个人资料的钩子吗?
我跑的Django 1.4使用Python 2.6的Enthought发行版,在Mac OS X(10.7.5)。
是否Django的测试数据库已经包含了所有的数据?我想,可能是错误的,测试数据库中的唯一数据是我包含在夹具中的数据。 – tchaymore