在Django中扩展抽象模型

时间:2018-11-22 18:08:19

标签: django django-2.0 django-oauth

我正在使用Django 2.xOAuth Toolkit

我正在编写一个应用程序,我需要添加指向ForeignKey插件的Application模型的OAuth Toolkit,该模型定义为 Abstract

根据the documentation。我可以扩展 AbstractApplication ,然后添加设置

OAUTH2_PROVIDER_APPLICATION_MODEL='your_app_name.MyApplication'

就我而言,我已经在 models.py

中创建了我的应用
class CustomOAuthToolkitApplication(AbstractApplication):
    pass


class OAuthToolkitTrackingLog(models.Model):

    application_model = settings.OAUTH2_PROVIDER_APPLICATION_MODEL

    application = models.ForeignKey(application_model, on_delete=models.CASCADE, blank=True, default=None, null=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, default=None)
    col_1 = models.CharField(max_length=255)

    created = models.DateTimeField(auto_now_add=True)

settings.py

INSTALLED_APPS = [
    ...
    'oauth2_provider',
    'my_app',
]

OAUTH2_PROVIDER_APPLICATION_MODEL = 'my_app.CustomOAuthToolkitApplication'

但是当我运行命令来生成迁移

python manage.py makemigrations

它给出错误为

The field oauth2_provider.Grant.application was declared with a lazy reference to 'my_app.customoauthtoolkitapplication', but app 'my_app' isn't installed.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'my_app.customoauthtoolkitapplication', but app 'my_app' isn't installed.

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

嗯。我不确定。但是据我从Django源代码记得的那样,INSTALLED_APPS中的顺序很重要。您能否尝试切换位置,所以:

INSTALLED_APPS = [
  ...
  'my_app',
  'oauth2_provider',
]

以便my_app在列表上更高?