无法在Django中迁移数据

gjmwrych  于 2023-04-13  发布在  Go
关注(0)|答案(1)|浏览(133)

我的Django结构如下:
https://studygyaan.com/django/best-practice-to-structure-django-project-directories-and-files#:~:text= The%20way%20I%20like%20to,content%20in%20the%20media%20文件夹.
我将我的应用sync_wits_ meta放在apps文件夹中,并将app_1添加到INSTALLED_APPS中

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
]

然后当我运行pythonmanage.pymakemigrations时,它显示错误:

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
w41d8nur

w41d8nur1#

__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'

相关问题