简体   繁体   中英

Getting name spaced models inside Django 1.7 migrations

I'm using django-allauth as part of a new project, and I'm trying to make a migration where I set up the initial authentication keys for the social apps.

To do this I need to access a model that is under a namespace of

'allauth.socialaccount', which I also have in my settings.py

However, when I attempt to do the following;

SocialApp = apps.get_model('allauth.socialaccount', 'socialapp')

I end up with Django telling me the app with that name doesn't exist.

I have also tried pretty much every combination of 'allauth', 'socialaccount' and, 'socialapp'

I'm pretty much stuck at this point.

For namespaced models you need to use the final part of the dotted path as the app_label . For allauth.socialaccount that is socialaccount .

>>> apps.get_model('socialaccount', 'SocialApp')
allauth.socialaccount.models.SocialApp

Also don't forget to reference the a migration from the socialaccount app as a dependency, otherwise it won't be available:

dependencies = [
    # ('some_app', 'XXXX_some_pre-requisite_migration'),
    ('socialaccount', '0001_initial'),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM