我想在 django 社交应用程序登录后添加服装字段



嗨,我是python/django新手的php开发人员

我正在使用"social-auth-app-django"库使用 django 创建一个社交登录名,我按照以下教程来实现它。

https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html

它工作正常,但我还需要在数据库中添加服装文件,这些文件将位于不同的表中,但在创建新用户时会添加它。 我扩展了用户表,如下所示

from django.contrib.auth.models import User
class NewsCreator(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
CreatorLastLogs= models.CharField(max_length=100)
CreatorLogs= models.CharField(max_length=100)

我想在创建新用户或现有用户登录时向这些字段添加数据。我尝试浏览文档,但找不到任何与代码扩展/自定义等相关的东西。提前致谢

嗨,我已经找到了答案,所以我为以后偶然发现这篇文章的人发帖。

Django Social提供了管道来扩展他们的代码,我们只需要扩展这个管道 为此,请在您的 setting.py 文件发布以下列表中(此列表中的所有方法都是默认管道方法,除了最后一个方法外,都会调用(。

SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
'newsapp.pipeline.save_profile'<-- this is your method
)

在你的应用中创建一个文件名为 pipeline.py 的文件,方法名称将在上面的列表中提供,就像列表中的最后一个字符串一样(新闻应用程序是我的应用的名称,提供你的应用名称(

在您的 pipeline.py 文件中

def save_profile(backend, user, response, *args, **kwargs):
if NewsCreator.objects.filter(user_id=user.id).count() == 0 :
newsCreator = NewsCreator.objects.create(user=user)
//your logic for new fields 
newsCreator.save()

如果你有任何其他关于Django-Social的问题,你可以参考 https://github.com/python-social-auth/social-docs 其详细文档

最新更新