Django OAuth Toolkit的自定义(可交换)模型

时间:2018-06-29 14:07:51

标签: django django-models django-migrations

我需要创建自定义Django OAuth Toolkit模型。 我有一个具有以下模型的应用程序:

Error

和设置:

from django.utils.translation import ugettext_lazy as _
from oauth2_provider.models import (
    AbstractApplication, AbstractGrant,
    AbstractAccessToken, AbstractRefreshToken
)


class Application(AbstractApplication):
    GRANT_TYPES = (
        (AbstractApplication.GRANT_AUTHORIZATION_CODE, _("Authorization code")),
        (AbstractApplication.GRANT_IMPLICIT, _("Implicit")),
    )


class Grant(AbstractGrant):
    pass


class AccessToken(AbstractAccessToken):
    pass


class RefreshToken(AbstractRefreshToken):
    pass

当我尝试进行迁移时,出现此错误:

INSTALLED_APPS = [
    ...
    'oauth2_provider',
    ...
    'apps.oauth.apps.OauthConfig',
    ...
]

OAUTH2_PROVIDER_GRANT_MODEL = 'oauth.Grant'
OAUTH2_PROVIDER_APPLICATION_MODEL = 'oauth.Application'
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL = 'oauth.AccessToken'
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = 'oauth.RefreshToken'

如果我删除ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'oauth.application', but app 'oauth' isn't installed. The field oauth2_provider.AccessToken.source_refresh_token was declared with a lazy reference to 'oauth.refreshtoken', but app 'oauth' isn't installed. The field oauth2_provider.Grant.application was declared with a lazy reference to 'oauth.application', but app 'oauth' isn't installed. The field oauth2_provider.RefreshToken.access_token was declared with a lazy reference to 'oauth.accesstoken', but app 'oauth' isn't installed. The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'oauth.application', but app 'oauth' isn't installed. 设置并再次运行makemigrations,我会遇到许多OAUTH2_PROVIDER_***_MODELfields.E304错误:

fields.E305

1 个答案:

答案 0 :(得分:0)

我和我的朋友做了以下事情: 要覆盖Django OAuth Toolkit使用的类, 没有像迁移和迁移这样的设置:

OAUTH2_PROVIDER_APPLICATION_MODEL = 'oauth2_provider.Application'
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = 'oauth2_provider.RefreshToken'

运行迁移后,删除或注释上述变量,然后将变量放入 指的是需要覆盖的类,在我们的例子中是这样的:

OAUTH2_PROVIDER_APPLICATION_MODEL = 'authentication.OAuthApplication'
OAUTH2_PROVIDER_GRANT_MODEL = 'authentication.OAuthGrant'
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL = 'authentication.OAuthAccessToken'
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = 'authentication.OAuthRefreshToken'

此链接https://django-oauth-toolkit.readthedocs.io/en/latest/advanced_topics.html#extending-the-application-model中包含有关为什么需要执行此过程的详细信息。