[英]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.