我正在使用最新版本的Django3和Python3来学习Django。
我想在我的Django项目中使用现有的sqlite3数据库。但是我在使用migration命令时遇到问题。
到目前为止,我对项目所做的步骤很少:
1。。使用venv,升级pip,安装Django,
2。。生成一个项目,
3。。插入一个应用,并将其添加到INSTALLED_APPS
,
4。。在settings.py
中,我将现有数据库添加到DATABASES
中,如下所示。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'testdb': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'TestDB.sqlite3'),
},
}
生成的models.py
看起来像:(我在其中添加了max_length=25
)
from django.db import models
class Stars(models.Model):
actorname = models.CharField(db_column='ActorName', blank=True, null=True, max_length=25)
realname = models.CharField(db_column='RealName', blank=True, null=True, max_length=25)
class Meta:
managed = False
db_table = 'Stars'
6。。然后运行python manage.py migrate
和/或python manage.py makemigrations
,在这里看到以下错误:
(venv) PS D:\Workfolder_Web\testProject\backend\project> 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 "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\Workfolder_Web\testProject\backend\venv\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 779, in exec_module
File "<frozen importlib._bootstrap_external>", line 916, in get_code
File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ValueError: source code string cannot contain null bytes
7。。我的数据库TestDB.sqlite3
非常简单,看起来像:
问题:如何调试?我当前的代码中没有使用任何引导程序,URL,视图或路由器。
感谢您的帮助!
答案 0 :(得分:0)
如果更改了模型,则可以为新字段添加默认值或删除数据库。