扩展Django Oauth2应用程序模型

时间:2019-02-15 14:11:38

标签: django django-models django-rest-framework django-views

我正尝试使用此link扩展Oauth2应用程序模型。

但是在迁移时出现了一些问题-

  

ValueError:字段oauth2_provider.AccessToken.application是   懒惰地声明   'oauth2.companyfieldaddedapplication',但应用程序'oauth2'不是   已安装。声明了oauth2_provider.Grant.application字段   对'oauth2.companyfieldaddedapplication'的懒惰引用,但是   未安装应用程序“ oauth2”。场   oauth2_provider.RefreshToken.application是用惰性声明的   引用“ oauth2.companyfieldaddedapplication”,但应用程序“ oauth2”   没有安装。

我在installed_apps中添加了oauth2,但仍显示错误

Settings.py

installed_apps = [
   'apps.oauth2',
]

OAUTH2_PROVIDER_APPLICATION_MODEL='oauth2.CompanyFieldAddedApplication'

我的项目结构

├── apollo_project
│   ├── __init__.py
│   ├── settings.py
│   ├── settings.py.ren
│   ├── urls.py
│   └── wsgi.py
├── apps
│   ├── oauth2
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0002_auto_20190215_1402.py
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py

project_folder>应用> oauth2> models.py

from django.db import models
from oauth2_provider.models import AbstractApplication
from apps.company.models import Company

class CompanyFieldAddedApplication(AbstractApplication):
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company')

追踪

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 163, in handle
    pre_migrate_apps = pre_migrate_state.apps
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/db/migrations/state.py", line 210, in apps
    return StateApps(self.real_apps, self.models)
  File "/home/codism-7/.local/share/virtualenvs/apollo_django-njoxc1BQ/lib/python3.5/site-packages/django/db/migrations/state.py", line 278, 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 'oauth2.companyfieldaddedapplication', but app 'oauth2' isn't installed.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'oauth2.companyfieldaddedapplication', but app 'oauth2' isn't installed.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'oauth2.companyfieldaddedapplication', but app 'oauth2' isn't installed.

3 个答案:

答案 0 :(得分:0)

尝试将'oauth2'添加到installed_apps

答案 1 :(得分:0)

只需从apps文件夹中删除您的应用程序,然后将其保留在它的外部即可。然后在您的INSTALLED_APPS中添加以下行。

installed_apps = [
   'oauth2.apps.Oauth2Config',
]

Oauth2Configclass中的apps.py

您的项目结构应如下所示

├── apollo_project
│   ├── __init__.py
│   ├── settings.py
│   ├── settings.py.ren
│   ├── urls.py
│   └── wsgi.py
├── apps
├── oauth2
     ├── admin.py
     ├── apps.py
     ├── __init__.py
     ├── models.py
     ├── tests.py
     ├── urls.py
     └── views.py

答案 2 :(得分:0)

我通过删除数据库中的所有表并进行迁移解决了问题。我不确定为什么会奏效,但确实能奏效。希望我能帮助别人。