4

I am trying to run the command python manage.py dumpdata > data.json

However, I receive such a traceback:

CommandError: Unable to serialize database: 'charmap' codec can't encode characters in position 1-4: character maps to <undefined>
Exception ignored in: <generator object cursor_iter at 0x0000020E11353820>
Traceback (most recent call last):
  File "C:\Users\Illia\Desktop\MyDjangoStuff\greatkart\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1625, in cursor_iter
    cursor.close()
sqlite3.ProgrammingError: Cannot operate on a closed database.


How to solve this issue?

illymarev
  • 237
  • 2
  • 13
  • Have you tried dumping with django-admin instead? – Tom Hamilton Stubber May 20 '21 at 09:17
  • It gets me a traceback saying that I need to define DJANGO_SETTINGS_MODULE first, but when I run django-admin dumpdata > json.data --settings greatkart.settings it says 'no module named "greatkart"', despite the fact it is the name of my project – illymarev May 20 '21 at 09:59

3 Answers3

13

Running set PYTHONIOENCODING=utf-8 before python manage.py dumpdata > data.json has solved the issue.

illymarev
  • 237
  • 2
  • 13
2

if this doesn't work, and if you are windows, try

python -Xutf8 ./manage.py dumpdata > data.json
Tony
  • 21
  • 3
-1

I couldn't get the soloution above to work, however i did get this to work;

https://pypi.org/project/django-dump-load-utf8/

I installed it and used its command for dumping data and it worked, hope this helps anybody else! I think my problem was that my database had Norwegian letters in it "ÆØÅ"

  • some context&explanations would be a minimum – LoneWanderer Oct 27 '22 at 17:18
  • I don't know what is worst, this answer or the linked package "django-dump-load-utf8" which the author says he was not able to contribute to Django itself and decided to create a new package because the Django's "contribution guide is too long to read". – kafran Oct 27 '22 at 17:33
  • Oh sorry, I legitimately meant what I said. The solution given on this subject did not solve foreign characters, however this package did. Just wanted to tell in case anyone else had the same problem. – Richard Max Oct 28 '22 at 19:55