jazzband / django-oauth-toolkit

OAuth2 goodies for the Djangonauts!
https://django-oauth-toolkit.readthedocs.io
Other
3.14k stars 794 forks source link

Django Lazy Reference ValueError after upgrading to the latest version and running migrations, #1120

Open smit-mehta25 opened 2 years ago

smit-mehta25 commented 2 years ago
    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'oauth.clientapplication', but app 'oauth' isn't installed.
The field oauth2_provider.AccessToken.source_refresh_token was declared with a lazy reference to 'oauth.clientrefreshtoken', but app 'oauth' isn't installed.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'oauth.clientapplication', but app 'oauth' isn't installed.
The field oauth2_provider.RefreshToken.access_token was declared with a lazy reference to 'oauth.clientaccesstoken', but app 'oauth' isn't installed.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'oauth.clientapplication', but app 'oauth' isn't installed.

I am constantly facing this issues. Is there any solutions for this one,

oauth2_provider setting configurations:

OAUTH2_PROVIDER_APPLICATION_MODEL = "oauth.ClientApplication"
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL = "oauth.ClientAccessToken"
OAUTH2_PROVIDER_GRANT_MODEL = "oauth.ClientGrant"
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = "oauth.ClientRefreshToken"
OAUTH2_PROVIDER_ID_TOKEN_MODEL = "oauth.ClientIdToken"
OAUTH2_PROVIDER = {
    "ACCESS_TOKEN_EXPIRE_SECONDS": 1800,
    "SCOPES": {
        "uid": "User ID read access",
    },
}

Even, I tried to apply run_before to my custom initial migrations. But No luck.

run_before = [
        ('oauth2_provider', '0001_initial'),
    ]

This is with Django==3.2.11 and django-oauth-toolkit==1.7.0

Below is the order of Apply migrations.

  Applying oauth.0001_initial_squashed_0004_auto_20220218_1009...accounts_ui client does not exist
accounts_ui client created
 OK
  Applying oauth2_provider.0001_initial... OK
  Applying oauth2_provider.0002_auto_20190406_1805... OK
  Applying oauth2_provider.0003_auto_20201211_1314... OK
  Applying oauth2_provider.0004_auto_20200902_2022... OK
  Applying oauth2_provider.0005_auto_20211222_2352... OK

But still i am facing the above error. I have tried everything that i could from other open issues. Such as ... Swappable models. Clean migrations, run before.

smit-mehta25 commented 2 years ago

634

n2ygk commented 2 years ago

@smit-mehta25 I assume your comment means this is a dup of #634?

undo-nothing commented 2 years ago

@smit-mehta25 same problem, i update to django 4.0.2 solved.

phith0n commented 2 years ago

similar issue when I run migration: ./manage.py migrate xxxx zero

my django version is 4.0.5:

ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
The field oauth2_provider.IDToken.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
n2ygk commented 2 years ago

@phith0n Can you be specific about what versions you are using? The issue was created back several months ago and also didn't reference the version. Please fill out this template:

Describe the bug

To Reproduce

Expected behavior

Version

Additional context

Also, I would ask that you repeat your reproducing test with Django 3.2 and with DOT 1.7.1 to help isolate if this is due to a change in Django or in DOT 2.x.

Thanks.

phith0n commented 2 years ago

Hi @n2ygk , I think I may have found the problem.

Refer to the document https://django-oauth-toolkit.readthedocs.io/en/latest/advanced_topics.html#AbstractApplication, everything is OK after I added run_before = [('oauth2_provider', '0001_initial'),] to the migrations that created ucenter.Application.

However, the error models.E022 stated in the documentation was not thrown.

n2ygk commented 2 years ago

I believe this run_before requirement is a symptom of the migrations not listing all the required dependencies on swappable models. In the initial migration, I suspect this in 0001_initial:

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL)
    ]

should be changed to this to make sure all possible swappable models become dependencies:

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        migrations.swappable_dependency(settings.APPLICATION_MODEL),
        migrations.swappable_dependency(settings.ACCESS_TOKEN_MODEL),
        migrations.swappable_dependency(settings.ID_TOKEN_MODEL),
        migrations.swappable_dependency(settings.GRANT_MODEL),
        migrations.swappable_dependency(settings.REFRESH_TOKEN_MODEL),
    ]

This has to do with auto-generated migrations (manage.py makemigrations) assuming the only possible swappable model is the User model as that's the "usual" case in other parts of Django.

I haven't had time to test this. Swappable models have been an ongoing point of confusion with this library forever.

See also #871, #778, #634 and https://github.com/openwisp/django-swappable-models (which I don't quite understand the point of).