Search code examples
pythondjangofacebookauthenticationdjango-socialauth

I want to add costume field after django social app login


Hi I am php developer new to python/django

I'm creating a social login with django using 'social-auth-app-django' library and i followed following tutorial to implement it.

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

Its working fine but i also need to add costume files in database which will be in different table but it will be get added when new user is created. I have extended the user table as following

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)

and i want to add data to these fields when a new user is created or when existing user logins. I tried going through documentation but could not found any thing that is related to code extension/customisation etc. Thanks in advance


Solution

  • Hi i have found answer to this so i'm posting for people who will stumble upon this post later.

    django social provides pipeline to extend their code, and we just have to extend this pipeline for this in your setting.py file post following list(all in this list are default pipeline methods which gets called except for last one).

    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
    )
    

    create a file in your app with name pipeline.py and name of method is to be provided in list above like last string in list (newsapp is name of my app provide your appname)

    in your pipeline.py file

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

    if you have any other query regarding django-social you can refer https://github.com/python-social-auth/social-docs its detail documentation