简体   繁体   中英

Django fails to loaddata with deserialization error

I am running Wagtail which runs on django and when running the dumpdata the following occurs

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

After that I delete the sqlite database and remove the migrations from the apps migrations directory so I can have an empty database and test the dumped data. After running migrate I execute loaddata dump1.json and the following error comes up

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': 

I have tried also to exclude some of the things i dont need while dumping, using the following

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

But it didnt make any difference.

The application is a simple blog following the demo shown here .

use --output instead of > to avoid sending console(stdout) debugging info to your dumped data, also exclude the irrelevant models

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

Based on your output, the dumped data is empty thus it throws the following error.

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

Ways to dump your data:

  1. Drop your database by deleting your sqlite database file
  2. Delete your migrations folder
  3. python manage.py makemigrations
  4. python manage.py migrate
  5. Try add some sample data by creating a user or other data
  6. Run dump data again python manage.py dumpdata -output my_dumps.json

Then, check the my_dumps.json file, if there are some data inside, then you can try load your data using python manage.py loaddata my_dumps.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM