Question

I've just integrated Django-Social-Auth (DSA) v0.7.23 using django 1.5 with a custom User model and auth/login is working fine except for the username field which is not being stored in my custom User model.

Stepping through the DSA code it appears to explicitly remove the username which was passed back by facebook. Here is the function in question that pops the username off:

@classmethod
def username_field(cls, values):
    user_model = cls.user_model()
    if hasattr(user_model, 'USERNAME_FIELD'):
        # Django 1.5 custom user model, 'username' is just for internal
        # use, doesn't imply that the model should have an username field
        values[user_model.USERNAME_FIELD] = values.pop('username') # Username removed
    return values

How can I go about getting the username that was passed back from facebook and not having it be explicitly removed by DSA?

I believe a work around would be to create a custom pipeline that generates a username. However I was wondering if anyone else encountered this scenario before and leveraged anything that already exists within DSA (i.e. a particular settings.py configuration)

Thanks.

Was it helpful?

Solution

The original username is available in the details attribute passed to pipeline functions, take this for example:

def generated_username(user, details, *args, **kwargs):
    username = details['username']
    user.your_field = username
    user.save()

It's worth noting that the username is popped from values if you have a USERNAME_FIELD defined in your custom model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top