简体   繁体   English

python-social-auth和Django,用自定义模型替换UserSocialAuth

[英]python-social-auth and Django, replace UserSocialAuth with custom model

I'm trying to integrate python-social-auth into an existent Django project. 我正在尝试将python-social-auth集成到现有的Django项目中。

I want to use an existent model for a user's social accounts, instead of UserSocialAuth (my DB already has data with it, as well as some custom fields). 我想为用户的社交帐户使用现有模型,而不是UserSocialAuth(我的数据库已经包含数据,以及一些自定义字段)。

Is there some setting for it? 它有一些设置吗?

My custom model looks like this: 我的自定义模型如下所示:

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()

Any ideas? 有任何想法吗?

Solved by creating a custom Storage : 通过创建自定义Storage解决:

# channels/models.py
# ...

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

And registering it in the settings: 并在设置中注册:

SOCIAL_AUTH_STORAGE = 'proj.channels.models.CustomSocialStorage'

For some reason this setting is documented only in the "Django" section of Python-social-auth's documentation, and not on the Settings page. 出于某种原因,此设置仅记录在Python-social-auth文档的“Django”部分中,而不是在“设置”页面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义用户 model 与 django 上的 python-social-auth - Custom user model with python-social-auth on django 如果我进行自定义管道,则Django和Python-Social-Auth重定向 - Django and Python-Social-Auth redirect if I make custom pipeline 使用自定义 User 模型和 UserManager 使用 python-social-auth 创建 django 用户的问题 - Issue creating a django user with python-social-auth with a custom User model and UserManager Python-Social-Auth在mongoEngine(Django)上失败 - Python-Social-Auth fails with mongoEngine (Django) 使用python-social-auth的自定义登录过程 - Custom login process with python-social-auth 使用python-social-auth的django社交认证错误 - django social authentication error using python-social-auth ImportError:无法导入名称social_auth_usersocialauth:通过shell访问Django中的Python社会认证数据 - ImportError: cannot import name social_auth_usersocialauth: Accessing Python social auth data in django via shell 使用python-social-auth将Django 1.6迁移到Django 1.10时出错 - Error migrating Django 1.6 to Django 1.10 with python-social-auth 无法运行python-social-auth示例django app - can't run the python-social-auth example django app 带有django错误的python-social-auth“没有这样的表:app_customuser” - python-social-auth with django errors with “no such table: app_customuser”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM