Django加载数据失败,出现反序列化错误

2024-10-02 14:26:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行在django上运行的Wagtail,当运行dumpdata时,会出现以下情况

python manage.py dumpdata --indent 2 --output dumps.json
CommandError: Unable to serialize database: no such table: wagtailimages_uploadedimage

之后,我删除sqlite数据库并从appsmigrations目录中删除迁移,这样我就可以拥有一个空数据库并测试转储的数据。在运行migrate之后,我执行loaddata dump1.json,出现以下错误

Tracking file by folder pattern:  migrations
Traceback (most recent call last):
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/serializers/json.py", line 68, in Deserializer
    objects = json.loads(stream_or_string)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/pycharm-professional/plugins/python/helpers/pycharm/django_manage.py", line 52, in <module>
    run_command()
  File "/opt/pycharm-professional/plugins/python/helpers/pycharm/django_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "/usr/lib/python3.8/runpy.py", line 207, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/user/Desktop/projects/mytestwebapp/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
    self.loaddata(fixture_labels)
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
    self.load_label(fixture_label)
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 172, in load_label
    for obj in objects:
  File "/home/user/Desktop/projects/mytestwebapp/venv/lib/python3.8/site-packages/django/core/serializers/json.py", line 73, in Deserializer
    raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/user/Desktop/projects/mytestwebapp/dump1.json': 

我也试着排除一些我在倾倒时不需要的东西,使用以下方法

dumpdata --natural-foreign --indent 2 -e contenttypes -e auth.permission -e wagtailcore.groupcollectionpermission -e wagtailcore.grouppagepermission -e wagtailimages.rendition -e sessions > dump1.json

但这没什么区别

该应用程序是一个简单的blog,演示如下here


Tags: djangoruninpycorejsonhomevenv
2条回答

使用 output而不是>避免向转储的数据发送控制台(stdout)调试信息,同时排除不相关的模型

python manage.py dumpdata  natural-foreign  indent 2 -e contenttypes -e auth.permission -e wagtailcore.groupcollectionpermission -e wagtailcore.grouppagepermission -e wagtailimages.rendition -e sessions -e wagtailimages.uploadedimage  output dumps.json

根据您的输出,转储的数据为空,因此会抛出以下错误

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

转储数据的方法:

  1. 通过删除sqlite数据库文件来删除数据库
  2. 删除您的迁移文件夹
  3. python manage.py makemigrations
  4. python manage.py migrate
  5. 尝试通过创建用户或其他数据来添加一些示例数据
  6. 再次运行转储数据python manage.py dumpdata -output my_dumps.json

然后,检查my_dumps.json文件,如果其中有一些数据,那么您可以尝试使用 python manage.py loaddata my_dumps.json

相关问题 更多 >