77👍
One solution is to use ./manage.py dumpdata -o data.json
instead of ./manage.py dumpdata > data.json
.
Another solution is to use Python’s UTF-8 mode, run:
python -Xutf8 ./manage.py dumpdata > data.json
9👍
To save json
data in django the TextIOWrapper is used:
The default encoding is now
locale.getpreferredencoding(False)
(…)
In documentation of locale.getpreferredencoding
fuction we can read:
Return the encoding used for text data, according to user preferences. User preferences are expressed differently on different systems, and might not be available programmatically on some systems, so this function only returns a guess.
Here I found "hacky" but working method to overwrite these settings:
In file settings.py
of your django project add these lines:
import _locale
_locale._getdefaultlocale = (lambda *args: ['en_US', 'utf8'])
- [Django]-ImportError: No module named crispy-forms
- [Django]-Simple search in Django
- [Django]-Dynamically creating classes – Python
4👍
Here is the solution from djangoproject.com
You go to Settings there’s a "Use Unicode UTF-8 for worldwide language support", box in "Language" – "Administrative Language Settings" – "Change system locale" – "Region Settings".
If we apply that, and reboot, then we get a sensible, modern, default encoding from Python.
djangoproject.com
the settings box looks like this. Enable the check and restart your system
- [Django]-Django: Difference between using server through manage.py and other servers like gunicorn etc. Which is better?
- [Django]-Get user profile in django
- [Django]-Django modelform NOT required field
1👍
On windows the way i solved mine was
Add to your settings
import _locale
_locale._getdefaultlocale = (lambda *args: ['en_US', 'utf8'])
run this on shell only on windows
python -Xutf8 manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
I actually did this it worked
python -Xutf8 manage.py dumpdata -o data.json
But wasnt showing data from my installed apps
- [Django]-Last_login field is not updated when authenticating using Tokenauthentication in Django Rest Framework
- [Django]-In Django, what is the most efficient way to check for an empty query set?
- [Django]-Django migration strategy for renaming a model and relationship fields
0👍
If you have multiple Python environments, before applying workarounds, it’s worth checking that you issue python manage.py
against the correct environment. That was my case where I met the same error: the database was created under containerized environment on Linux, with a higher Python version, but Django and other packages were present also in the legacy local environment on Windows. As well, the project directory was attached to the container as a volume, and the content was identical locally and in the container. So, I just mix up and ran manage.py
locally instead of being attached to the container.
- [Django]-Why do I get "ImportError: cannot import name find_spec" when I start a new Django project?
- [Django]-Django migrations – workflow with multiple dev branches
- [Django]-Django.db.utils.OperationalError: fe_sendauth: no password supplied
0👍
I think this is the current best way: In the directory Python311\Lib\site-packages\django\core\serializers
, you open the json.py
file. In the json.py file
, find the section:
class Serializer(PythonSerializer):
.................
def _init_options(self):
.............
# Default is False
self.json_kwargs.setdefault("ensure_ascii", False)
# You fix False to True
self.json_kwargs.setdefault("ensure_ascii", True)
- [Django]-How can I upload multiple files to a model field?
- [Django]-How can I set a default value for a field in a Django model?
- [Django]-Django multi-level template extends and nested blocks