Django:加载数据很困难

时间:2010-04-30 19:26:25

标签: python django json

以下是我尝试加载数据的一些模型:

class School(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class RequirementSet(models.Model):
    offeringSchool = models.ForeignKey(School)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return "%s at %s" % (self.name, self.offeringSchool)

这是我试图用来初始化它们的JSON:

// data.json

[
    {
        "pk": "1",
        "model": "myapp.School",
        "fields":
        {
            "name": "Princeton"
        }
    },
    {
        "pk": "1",
        "model": "myapp.RequirementSet",
        "fields":
        {
            "name": "Computer Science",
            "offeringSchool": 1
        }
    }
]

我运行python manage.py loaddata data.json,我收到此错误:

...
Checking absolute path for fixtures...
Trying absolute path for data.json fixture 'data'...
Installing json fixture 'data' from absolute path.
Problem installing fixture 'data.json': Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\django\core\management\commands\loaddata.p
y", line 150, in handle
    for obj in objects:
  File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41,
 in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream)):
  File "C:\Python26\lib\json\__init__.py", line 267, in load
    parse_constant=parse_constant, **kw)
  File "C:\Python26\lib\json\__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "C:\Python26\lib\json\decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python26\lib\json\decoder.py", line 338, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我做错了什么?

通过shell和管理UI与这些模型交互工作正常。

1 个答案:

答案 0 :(得分:3)

JSON格式不允许有评论。删除// data.json行,它应该可以正常工作。