无法在Django中迁移数据



我的Django结构遵循本文:

https://studygyaan.com/django/best-practice-to-structure-django-project-directories-and-files#:~:text=%20方式%20I%20点赞%20到,内容%20在%20媒体%20文件夹中。

我将应用程序sync_wits_meta放在应用程序文件夹中的位置并在INSTALLED_APPS 中添加app_1

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.sync_wits_meta', # <--- here
]

然后,当我运行python manage.py makemigrations时,它显示错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 210, in create
app_module = import_module(app_name)
File "/usr/local/lib/python3.8/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 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'sync_wits_meta'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 420, in execute
django.setup()
File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 212, in create
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'sync_wits_meta'. Check that 'apps.sync_wits_meta.apps.SyncWitsMetaConfig.name' is correct.
The command '/bin/sh -c python manage.py makemigrations apps/sync_wits_meta' returned a non-zero code: 1

如果我将应用程序放在应用程序文件夹中,如何进行迁移?

我使用的是Django 4.1.7版本。


更新:

我的项目结构如下:

sync_service 
sync_service
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
apps
__init__.py
sync_wits_meta
__init.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
manage.py
requirement.txt

__init.pysync_wits_meta内的名称不正确。将其更改为__init__.py。如果这不起作用,这里有一个更长的解决方案:

不要只提到项目名称,而是为它指定一个AppConfig类。在INSTALLED_APP中编写apps.sync_wits_meta.apps.SyncWitsMetaConfig,在apps/sync_wits_meta/app.py中添加一个继承AppConfig类的小类,如下所示:

class SyncWitsMetaConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'sync_wits_meta'

最新更新