理想情况下,您的第三方依赖项将在配置中包含此行apps.py
:
from django.apps import AppConfig
class ExampleConfig(AppConfig):
default_auto_field = 'django.db.models.AutoField'
在等待上游依赖项更新它们的同时apps.py
或迁移文件,您可以自己覆盖应用程序配置。如果尚不存在,请创建一个apps.py
您的主应用程序目录中的文件(例如:project/apps.py
),并覆盖依赖项的配置。在此示例中,我将覆盖以下配置django-allauth
:
from allauth.account.apps import AccountConfig
from allauth.socialaccount.apps import SocialAccountConfig
class ModifiedAccountConfig(AccountConfig):
default_auto_field = 'django.db.models.AutoField'
class ModifiedSocialAccountConfig(SocialAccountConfig):
default_auto_field = 'django.db.models.AutoField'
然后修改INSTALLED_APPS
in settings.py
看起来像这样,替换旧条目django-allauth
在这个例子中:
INSTALLED_APPS = [
# ....
# replace: "allauth.account", with
"projectname.apps.ModifiedAccountConfig",
# replace: "allauth.socialaccount", with
"projectname.apps.ModifiedSocialAccountConfig",
]
如果依赖项没有apps.py
要覆盖的文件,您仍然可以创建一个AppConfig
子类在project/apps.py
像这样:
from django.apps import AppConfig
class ModifiedExampleDependencyConfig(AppConfig):
name = 'exampledependency' # the python module
default_auto_field = 'django.db.models.AutoField'
现在当你跑步时python manage.py makemigrations
,不应为依赖项创建迁移文件。