Django-oauth-toolkit: Migration dependency issue if extending AbstractApplication

Created on 21 Jan 2020  路  6Comments  路  Source: jazzband/django-oauth-toolkit

If I extend AbstractApplication from oauth2_provider to make a custom application_model say
custom.Application , then the migrations of this custom model should run prior too the oauth2_provider.migrations.0001_inital.py as this migration uses the fields from the custom models.
But after https://github.com/jazzband/django-oauth-toolkit/pull/531 merge, this dependency is removed, so while running migrations, custom Model's migrations are not running before the library's internal migration giving error like

   raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
   ValueError: Related model 'custom.Application' cannot be resolved

Most helpful comment

Hello!

Today I did workaround, it's simply.

  1. To django settings add this:
MIGRATION_MODULES = {
    'oauth2_provider': 'oauth2_lib.migrations'
}
  1. Create package oauth2_lib.migrations
  2. Copy migrations from cp ~/venv36/lib/python3.6/site-packages/oauth2_provider/migrations/* oauth2_lib/migrations
  3. Change oauth2_lib/migrations/0001_initial.py dependencies into
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        migrations.swappable_dependency(oauth2_settings.APPLICATION_MODEL), 
        migrations.swappable_dependency(oauth2_settings.ACCESS_TOKEN_MODEL),    
        migrations.swappable_dependency(oauth2_settings.REFRESH_TOKEN_MODEL),   
        migrations.swappable_dependency(oauth2_settings.GRANT_MODEL),
    ]

@RenanMarcell up

All 6 comments

Same here ;(

Since the new release 1.1.0 is impossible to extend AbstractApplication from the models to create a customized one without having migration problems. The problem persists even to the newest version. Does anyone have the same problem and how are they solving this issue?

+1

Hello!

Today I did workaround, it's simply.

  1. To django settings add this:
MIGRATION_MODULES = {
    'oauth2_provider': 'oauth2_lib.migrations'
}
  1. Create package oauth2_lib.migrations
  2. Copy migrations from cp ~/venv36/lib/python3.6/site-packages/oauth2_provider/migrations/* oauth2_lib/migrations
  3. Change oauth2_lib/migrations/0001_initial.py dependencies into
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        migrations.swappable_dependency(oauth2_settings.APPLICATION_MODEL), 
        migrations.swappable_dependency(oauth2_settings.ACCESS_TOKEN_MODEL),    
        migrations.swappable_dependency(oauth2_settings.REFRESH_TOKEN_MODEL),   
        migrations.swappable_dependency(oauth2_settings.GRANT_MODEL),
    ]

@RenanMarcell up

hey @Sharpek .. I tried your solution and couldn't get it to work. I got several other errors, but that's another issue.

What I'm worried about new migrations that django-oauth-toolkit might add. You'll have to keep updating the migrations manually by copying them from your virtualenv?

Made it work here.
From the docs

Be aware that, when you intend to swap the application model, you should create and run the migration defining the swapped application model prior to setting OAUTH2_PROVIDER_APPLICATION_MODEL. You鈥檒l run into models.E022 in Core system checks if you don鈥檛 get the order right.

You can force your migration providing the custom model to run in the right order by adding:

run_before = [
    ('oauth2_provider', '0001_initial'),
]
to the migration class.

That鈥檚 all, now Django OAuth Toolkit will use your model wherever an Application instance is needed.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bastbnl picture bastbnl  路  3Comments

jlhawn picture jlhawn  路  3Comments

EmilTemirov picture EmilTemirov  路  5Comments

IvanAnishchuk picture IvanAnishchuk  路  6Comments

clintonb picture clintonb  路  6Comments