[Answer]-Django social auth not returning first_name and last_name when creating a profile – django 1.5

1👍

Django-social-auth doesn’t work like that when creating user, instead when calling create_user in your manager, it just passes the username and the email as you can see here. Later in the pipeline more fields in your user model are update when this is called, but as the code says, it does a getattr(user, name, None) which in you case for first_name and last_name, returns None since those fields aren’t defined in your model.

You can trick the system by defining some properties in your model, something like this:

class User(...):
    @property
    def first_name(self):
        if not hasattr(self, '_first_name'):
            self._first_name = self.full_name.split(' ', 1)[0]
        return self._first_name

    @first_name.setter
    def first_name(self, value):
        self._first_name = value
        self.build_full_name()

    @property
    def last_name(self):
        if not hasattr(self, '_last_name'):
            self._last_name = self.full_name.split(' ', 1)[-1]
        return self._last_name

    @first_name.setter
    def last_name(self, value):
        self._last_name = value
        self.build_full_name()

    def build_full_name(self):
        self.full_name = ' '.join([self._first_name, self._last_name])
👤omab

Leave a comment