Reputation: 1056
I'm working with Django 3.0 and after playing a bit with this framework, I would like to connect it to my big MySQL Database. My goal is to be able to set up the Admin Django Dashboard with my all my data from my MySQL Database.
So far, after seeing some tutorials, this is what I've done :
Settings.py
DATABASES = {
'default': {
'ENGINE': "django.db.backends.mysql",
'NAME': "nameDB",
'USER': "root",
'PASSWORD': "mypassword",
'HOST': "myHost",
'PORT': "",
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
My App (let's say dashboard) is installed on my INSTALLED_APPS.
Then, I did :
python manage.py inspectdb > models.py
It generate well the models.py. So I moved the models.py to my Dashboard app folder. After that, I wanted to migrate my DB using this following command line :
python manage.py migrate
At this step, there are some errors that I'm not able to solve.
Errors
Traceback (most recent call last):
File ".\manage.py", line 21, in <module>
main()
File ".\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
(base) PS C:\xampp\htdocs\django-test\app> python .\manage.py migrate
Traceback (most recent call last):
File ".\manage.py", line 21, in <module>
main()
File ".\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\nameUser\Anaconda3\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\nameUser\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 724, in exec_module
File "<frozen importlib._bootstrap_external>", line 860, in get_code
File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ValueError: source code string cannot contain null bytes
I did
Python manage.py check
And I got the same errors. I cannot do anything until I removed my dashboard app.
I precise that I did the same thing with a new empty MySQL Database and it worked well. I was able to create a super user for Admin and go into the Admin Dashboad.
So, I think there is a problem with my current MySQL Database but I don't know what.
Could you please help me on that please ?
Upvotes: 1
Views: 88
Reputation: 1056
After hours of struggling, I was able to find the issue.
When you run the command
python manage.py inspectdb > models.py
The file is encoded to UCS-2 which is not utf-8 format. So I changed the encoding to utf-8 and I don't have this error anymore. The question is : how to force the inspectdb command to encode in utf-8 ?
But well, my problem is solved so far.
Upvotes: 1