如何修复装载夹具错误

时间:2015-07-06 20:58:20

标签: python django django-fixtures

我正在尝试将初始数据导入我的Django应用程序。我有几个表,但我想逐个导入它们。

我决定从最简单的表开始:

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

它返回:

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作为后端数据库。

1 个答案:

答案 0 :(得分:6)

您的灯具无效JSON。与Python不同,JSON不能使用尾随逗号。