如何修复负载夹具错误
How to fix a load fixture error
我正在尝试将初始数据导入我的 Django 应用程序。我有几个 table,但我想一个一个导入它们。
我决定从最简单的开始 table:
class ClientRun(models.Model):
start_time = models.BigIntegerField()
end_time = models.BigIntegerField()
ip = models.GenericIPAddressField()
country = models.CharField(max_length=255)
def __unicode__(self):
return str(self.start_time) + " " + str(self.end_time)
并且我手动创建了一个夹具文件:
[
{
"model": "georoute.clientrun",
"pk": 1,
"fields": {
"ip": "0.0.0.0",
"start_time": 0,
"end_time": 0,
"country": "ZZ"
}
},
]
当我运行
python manage.py loaddata shengy_clientrun.json
它returns:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 90, in loaddata
self.load_label(fixture_label)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 141, in load_label
for obj in objects:
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 84, in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 78, in Deserializer
for obj in PythonDeserializer(objects, **options):
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/python.py", line 109, in Deserializer
for (field_name, field_value) in six.iteritems(d["fields"]):
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/shengy/Dropbox/shengy/code/django_tutorial/gae_site/georoute/fixtures/shengy_clientrun.json': u'fields'
我仔细检查了 JSON 格式,数据库现在是空的。我正在使用 MySQL 作为后端数据库。
您的夹具无效 JSON。与 Python 不同,JSON 不能有尾随逗号。
我正在尝试将初始数据导入我的 Django 应用程序。我有几个 table,但我想一个一个导入它们。
我决定从最简单的开始 table:
class ClientRun(models.Model):
start_time = models.BigIntegerField()
end_time = models.BigIntegerField()
ip = models.GenericIPAddressField()
country = models.CharField(max_length=255)
def __unicode__(self):
return str(self.start_time) + " " + str(self.end_time)
并且我手动创建了一个夹具文件:
[
{
"model": "georoute.clientrun",
"pk": 1,
"fields": {
"ip": "0.0.0.0",
"start_time": 0,
"end_time": 0,
"country": "ZZ"
}
},
]
当我运行
python manage.py loaddata shengy_clientrun.json
它returns:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 90, in loaddata
self.load_label(fixture_label)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 141, in load_label
for obj in objects:
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 84, in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 78, in Deserializer
for obj in PythonDeserializer(objects, **options):
File "/usr/local/lib/python2.7/site-packages/django/core/serializers/python.py", line 109, in Deserializer
for (field_name, field_value) in six.iteritems(d["fields"]):
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/shengy/Dropbox/shengy/code/django_tutorial/gae_site/georoute/fixtures/shengy_clientrun.json': u'fields'
我仔细检查了 JSON 格式,数据库现在是空的。我正在使用 MySQL 作为后端数据库。
您的夹具无效 JSON。与 Python 不同,JSON 不能有尾随逗号。