我使用以下命令生成了2个灯具:
./manage.py dumpdata --format=json --indent=4 --natural auth.User > fixtures/user.json
./manage.py dumpdata --format=json --indent=4 --natural --exclude=contenttypes --exclude=auth > fixtures/full.json
我有以下名为user.json的装置:
[
{
"pk": 4,
"model": "auth.user",
"fields": {
"username": "foo",
"first_name": "Se\u00e1n",
"last_name": "Hayes",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2010-09-27 21:57:45",
"groups": [],
"user_permissions": [],
"password": "!",
"email": "[email protected]",
"date_joined": "2010-09-27 21:57:45"
}
}
]
以及以下名为full.json的装置:
[
{
"pk": "72a75887b4a0ce06a61f9183fe1c0e15",
"model": "sessions.session",
"fields": {
"expire_date": "2010-10-11 21:57:45",
"session_data": "gAJ9cQEoVRJfYXV0aF91c2VyX2JhY2tlbmRxAlUOZmIuYXV0aC5GYkF1dGhxA1UNX2F1dGhfdXNl\ncl9pZHEEigEEdS5hOGZlODU0MmRjYmUwNmEzODIwNjhiYzYyODc2MWQxZA==\n"
}
},
{
"pk": 1,
"model": "sites.site",
"fields": {
"domain": "example.com",
"name": "example.com"
}
},
{
"pk": 2,
"model": "common.userprofile",
"fields": {
"money": 10,
"energy": 10,
"experience": 0,
"stamina": 10,
"health": 10,
"user": 4
}
},
{
"pk": 2,
"model": "missions.missionprofile",
"fields": {
"user": 4,
"last_area_viewed": null
}
},
{
"pk": 1,
"model": "fb.facebookuser",
"fields": {
"updated": "2010-09-27 21:57:45",
"uid": "24411841",
"created": "2010-09-27 21:57:45",
"access_token": "foo",
"url": "http://www.facebook.com/profile.php?id=24411841",
"user": 4,
"img_url": null,
"name": "Se\u00e1n Hayes"
}
}
]
运行以下命令(以任何顺序):
./manage.py loaddata user
./manage.py loaddata full
引发以下异常:
Problem installing fixture '/projectpath/fixtures/full.json': Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/django/core/management/commands/loaddata.py", line 169, in handle
obj.save(using=using)
File "/usr/lib/pymodules/python2.6/django/core/serializers/base.py", line 165, in save
models.Model.save_base(self.object, using=using, raw=True)
File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 528, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 1479, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 783, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 727, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.6/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry '4' for key 'user_id'")
有什么想法吗?我知道MySQL中的前向引用存在问题,但是如果在包含外键的固定装置之前安装了ID为4的User对象没关系,对吗?
最佳答案
根据Ashok的评论,当我遇到相同的问题时,可以通过更改信号处理程序以检查其是否以“原始”模式运行来解决,这显然意味着正在加载灯具:
def create_user_profile(sender, instance, created, **kwargs):
if created and not kwargs.get('raw', False):
UserProfile.objects.create(user=instance)
参见How do I prevent fixtures from conflicting with django post_save signal code?