Skip to main content
 首页 » 编程设计

python之Django : ValueError on loading fixture

2025年05月04日25落叶无声

我正在尝试加载一个如下所示的夹具:

some_filename.json

[ 
{ 
    "fields": { 
        "blockName": "Home & Tech",  
        "catName": "Home",  
        "catUrl": "http://www.buyam.com.ng/catalog/home/35",  
        "prodDesc": "Jojo Pet food is made with added nutrition to meet the demands of a grown up dog that's highly active and needs loads of energy and nutrition. By gaining the required energy with this food, dogs can live life to the optimum. This food can prove to be better as it gives dogs the required blend of fibers that can keep the stomach healthy and the patented X-shaped kibbles that are good to maintain clean and strong teeth till the gum line.",  
        "prodName": "Jo-Jo Dog Food - Tripe Mix 400g",  
        "prodTypeName": "Pet Supplies",  
        "prodTypeUrl": "http://www.buyam.com.ng/catalog/pet-supplies/419",  
        "prodUrl": "http://www.buyam.com.ng/catalog/pet-supplies/419/jo-jo-dog-food-tripe-mix-400g" 
    },  
    "model": "kudisavers.product",  
    "pk": 1 
}, 
{ 
... 
... 
} 
] 

但是我得到以下错误:

    Traceback (most recent call last): 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\serializers\json.py", line 74, in Deserializer 
    objects = json.loads(stream_or_string) 
  File "C:\Python34\lib\json\__init__.py", line 318, in loads 
    return _default_decoder.decode(s) 
  File "C:\Python34\lib\json\decoder.py", line 343, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode 
    raise ValueError(errmsg("Expecting value", s, err.value)) from None 
ValueError: Expecting value: line 1 column 1 (char 0) 
 
During handling of the above exception, another exception occurred: 
 
Traceback (most recent call last): 
  File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line 
    utility.execute() 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\__init__.py", line 377, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv 
    self.execute(*args, **options.__dict__) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\base.py", line 338, in execute 
    output = self.handle(*args, **options) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\commands\loaddata.py", line 61, in handle 
    self.loaddata(fixture_labels) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\commands\loaddata.py", line 91, in loaddata 
    self.load_label(fixture_label) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\management\commands\loaddata.py", line 142, in load_label 
    for obj in objects: 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\serializers\json.py", line 81, in Deserializer 
    six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2]) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\utils\six.py", line 549, in reraise 
    raise value.with_traceback(tb) 
  File "D:\MyWork\Python\Django_Projects\VirtualEnv\lib\site-packages\django\core\serializers\json.py", line 74, in Deserializer 
    objects = json.loads(stream_or_string) 
  File "C:\Python34\lib\json\__init__.py", line 318, in loads 
    return _default_decoder.decode(s) 
  File "C:\Python34\lib\json\decoder.py", line 343, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode 
    raise ValueError(errmsg("Expecting value", s, err.value)) from None 
django.core.serializers.base.DeserializationError: Problem installing fixture 'D:\MyWork\Python\Django_Projects\kudisavers\fixtures\buyam-fixture.json': Expecting value: line 1 column 1 (char 0) 

这就是我创建 json 的方式:

file = open('some_filename.json', 'wb') 
file.write(json.dumps(data, sort_keys=True, indent=4)) 
file.close() 

然后我像这样从 cmd 加载夹具:

python manage.py loaddata some_filename.json  

值和键对我来说似乎没问题。整个json中没有空值。 可能出了什么问题? 谢谢:)

请您参考如下方法:

您应该删除 json 格式,正确的 json 文件如下所示:

[{"pk": 1, "model": "contenttypes.contenttype", "fields": {"model": "permission", "name": "permission", "app_label": "auth"}},...]