我使用以下命令生成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": "foo@gmail.com",
"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对象,那么这应该没关系,对吗?
答案 0 :(得分:15)
根据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?
答案 1 :(得分:0)
Django的静态装置是一个反模式,这就是你遇到这种问题的原因。我建议你使用一些为你生成这个灯具的库,包括ID自然。