我正在尝试加载看起来像这样的灯具:
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中没有空值。 可能有什么问题? 谢谢:))
答案 0 :(得分:1)
你应该删除json格式,正确的json文件如下所示:
[{"pk": 1, "model": "contenttypes.contenttype", "fields": {"model": "permission", "name": "permission", "app_label": "auth"}},...]
答案 1 :(得分:0)
您应该使用dumpdata来创建灯具。
来自文档:
输出标准输出与指定应用程序关联的数据库中的所有数据。
如果未提供应用程序名称,则将转储所有已安装的应用程序。
dumpdata的输出可以用作loaddata的输入。
请注意,dumpdata使用模型上的默认管理器来选择要转储的记录。如果您使用自定义管理器作为默认管理器并且它过滤了一些可用记录,则不会转储所有对象。
可以提供--all选项来指定dumpdata应该使用Django的基本管理器,转储可能由自定义管理器过滤或修改的记录。