簡體   English   中英

Django 無法加載數據並出現反序列化錯誤

[英]Django fails to loaddata with deserialization error

我正在運行在 django 上運行的dumpdata ,並且在運行轉儲數據時會發生以下情況

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

之后,我刪除 sqlite 數據庫並從應用程序migrations目錄中刪除遷移,這樣我就可以擁有一個空數據庫並測試轉儲的數據。 運行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

但它沒有任何區別。

該應用程序是一個簡單的博客,遵循 此處顯示的演示。

使用--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

根據您的 output,轉儲的數據為空,因此會引發以下錯誤。

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加載數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM