Django迁移报错:oauth2_provider字段引用未安装的apis应用
Django使用django-oauth-toolkit自定义Application模型迁移报错解决
环境信息
- Django版本:4.0.6
- django-oauth-toolkit版本:2.2.0
项目结构
accs cms apis migrations models __init__.py custom_application.py static templates views __init__.py apps.py urls.py owner sonline tnt
自定义Application模型代码(apis/models/custom_application.py)
from django.db import models from oauth2_provider.models import AbstractApplication from tnt.models import TntModel class CustomApplicationModel(AbstractApplication): tnt = models.ForeignKey(TntModel, on_delete=models.CASCADE) class Meta(AbstractApplication.Meta): pass
项目配置(sonline/settings.py)
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', 'cms', 'accs', 'tnt', 'owner', 'rest_framework', 'background_task', 'apis', 'oauth2_provider', ] # OAUTH2 settings OAUTH2_PROVIDER = { 'ACCESS_TOKEN_EXPIRE_SECONDS': 604800 } OAUTH2_PROVIDER_APPLICATION_MODEL = 'apis.CustomApplicationModel'
错误场景及日志
执行makemigrations成功生成apis的迁移文件,但执行migrate时报错:
(venv) PS D:\s> python .\manage.py makemigrations Migrations for 'apis': apis\migrations\0001_initial.py - Create model CustomApplicationModel (venv) PS D:\s> python .\manage.py migrate Operations to perform: Apply all migrations: accs, admin, auth, background_task, cms, contenttypes, oauth2_provider, apis, sessions, tnt Traceback (most recent call last): File "D:\s\manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "D:\venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line utility.execute() File "D:\venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\venv\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv self.execute(*args, **cmd_options) File "D:\venv\lib\site-packages\django\core\management\base.py", line 460, in execute output = self.handle(*args, **options) File "D:\venv\lib\site-packages\django\core\management\base.py", line 98, in wrapped res = handle_func(*args, **kwargs) File "D:\venv\lib\site-packages\django\core\management\commands\migrate.py", line 236, in handle pre_migrate_apps = pre_migrate_state.apps File "D:\venv\lib\site-packages\django\utils\functional.py", line 49, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "D:\venv\lib\site-packages\django\db\migrations\state.py", line 544, in apps return StateApps(self.real_apps, self.models) File "D:\venv\lib\site-packages\django\db\migrations\state.py", line 615, in __init__ raise ValueError("\n".join(error.msg for error in errors)) ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'apis.customapplicationmodel', but app 'apis' isn't installed. The field oauth2_provider.Grant.application was declared with a lazy reference to 'apis.customapplicationmodel', but app 'apis' isn't installed. The field oauth2_provider.IDToken.application was declared with a lazy reference to 'apis.customapplicationmodel', but app 'apis' isn't installed. The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'apis.customapplicationmodel', but app 'apis' isn't installed.
解决方案
1. 确保自定义模型被Django识别
编辑apis/models/__init__.py,导入自定义模型:
from .custom_application import CustomApplicationModel
Django需要从apis.models路径下读取到该模型,否则会判定模型不存在。
2. 重置并重新生成迁移文件
删除已生成的apis迁移文件(apis/migrations/0001_initial.py),然后重新生成:
python manage.py makemigrations apis
3. 优先执行apis应用的迁移
先创建自定义Application模型对应的表,避免oauth2_provider迁移时找不到依赖:
python manage.py migrate apis
4. 执行全局迁移
此时oauth2_provider的迁移可以正常引用已创建的CustomApplicationModel:
python manage.py migrate
额外处理(若之前执行过oauth2_provider迁移)
如果数据库中已有oauth2_provider的旧表结构,需要先回滚其迁移记录:
python manage.py migrate oauth2_provider zero
再按上述步骤重新执行迁移。
内容的提问来源于stack exchange,提问作者Remus Case
相关产品推荐
相关产品推荐

