【问题标题】:python-social-auth and Django, replace UserSocialAuth with custom modelpython-social-auth 和 Django,用自定义模型替换 UserSocialAuth
【发布时间】:2014-10-21 02:21:25
【问题描述】:

我正在尝试将 python-social-auth 集成到现有的 Django 项目中。

我想为用户的社交帐户使用现有模型,而不是 UserSocialAuth(我的数据库已经有数据,以及一些自定义字段)。

有什么设置吗?

我的自定义模型如下所示:

class Channel(models.Model, DjangoUserMixin):
    PROVIDER_CHOICES = (
        ('twitter', 'Twitter'),
    )

    uid = models.CharField(max_length=255)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
                             related_name='channels')

    provider = models.CharField(max_length=32, choices=PROVIDER_CHOICES)
    extra_data = JSONField()

    class Meta:
        unique_together = ('provider', 'uid')

    @classmethod
    def get_social_auth(cls, provider, uid):
        try:
            return cls.objects.select_related('user').get(provider=provider, uid=uid)
        except Channel.DoesNotExist:
            return None

    username_max_length = 255
    user_model = get_user_model()

有什么想法吗?

【问题讨论】:

  • 我开了一张票来支持扩展UserSocialAuth 模型。您不必在新模型中复制/粘贴所有现有的 UserSocialAuth 模型。它应该是一个可以扩展的抽象类。这样您就可以在利用现有模型的同时添加您想要的任何新字段:github.com/omab/python-social-auth/issues/698

标签: python django django-models python-social-auth


【解决方案1】:

通过创建自定义Storage解决:

# channels/models.py
# ...

class CustomSocialStorage(DjangoStorage):
    """To replace UserSocialAuth model with Channel"""
    user = Channel

并在设置中注册:

SOCIAL_AUTH_STORAGE = 'proj.channels.models.CustomSocialStorage'

出于某种原因,此设置仅记录在 Python-social-auth 文档的 "Django" section 中,而不是设置页面上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多