我在 Django 项目中使用 django-oauth-toolkit 库时遇到 Django 迁移问题。错误消息指出字段 oauth2_provider.AccessToken.application 是使用对“apis.customapplicationmodel”的惰性引用声明的,但未安装应用程序“apis”。
以下是有关我的项目设置的一些信息:
Django version: 4.0.6
django-oauth-toolkit version: 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
自定义应用程序.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
settings.py,位于
sonline
目录
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'
当我运行以下命令时,它给出以下错误:
(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.
(venv) PS D:\s>
我想做什么?: 我需要在
oauth2_provider_application
表中添加一个属性,您可以在 custom_application.py
中看到,我正在添加引用 TntModel 的 tnt 属性。
你能帮我一下吗?预先感谢您。
有同样的错误 我删除所有
migrations
文件并截断数据库中的 django_migrations
表
然后再次运行 makemigrations
和 migrate
.