【问题标题】:Getting name spaced models inside Django 1.7 migrations在 Django 1.7 迁移中获取名称空间模型
【发布时间】:2015-03-25 22:40:02
【问题描述】:

我正在使用 django-allauth 作为新项目的一部分,并且我正在尝试进行迁移,为社交应用设置初始身份验证密钥。

为此,我需要访问位于命名空间下的模型

'allauth.socialaccount',我的 settings.py 中也有这个

但是,当我尝试执行以下操作时;

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

最后,Django 告诉我同名的应用不存在。

我也尝试了几乎所有“allauth”、“socialaccount”和“socialapp”的组合

我几乎被困在这一点上。

【问题讨论】:

    标签: python django django-models django-1.7 django-migrations


    【解决方案1】:

    对于命名空间模型,您需要使用虚线路径的最后部分作为app_label。对于allauth.socialaccount,即socialaccount

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

    另外不要忘记引用来自socialaccount 应用程序的迁移作为依赖项,否则它将不可用:

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

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2014-12-13
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 2016-03-26
      • 2015-10-31
      相关资源
      最近更新 更多