python - 我想在 django 社交应用登录后添加自定义字段

标签 python django facebook authentication django-socialauth

嗨,我是 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)

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

最佳答案

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

djangosocial提供了管道来扩展他们的代码,我们只需要扩展这个管道 为此,请在您的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 的文件,方法名称将在上面的列表中提供,如列表中的最后一个字符串(newsapp 是我的应用程序的名称,提供您的应用程序名称)

在 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 其详细文档

关于python - 我想在 django 社交应用登录后添加自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52913916/

相关文章:

python - 集会 python REST : Query all tasks from chosen iteration

python - 在 Windows 10 中找不到 Heroku 命令

Facebook 登录 - 应用程序委托(delegate)中的 Sigabrt 错误和应用程序崩溃

facebook - 我可以将用户从个人 Facebook 页面无缝重定向到企业 Facebook 页面吗?

python - 在不更改缩进或添加注释的情况下停用 python 中的代码

python - Pandas:推断列名称的智能方法

django - 重命名 Django 迁移文件是否安全?

python - 如何通过 django 属性订购,或替代解决方案

python - 从多个事件源中删除事件

django - 无法实现django-rules授权