I am using DRF and for login/registration I am using Django-rest-auth.
It seems like django-allauth doesn't allow saving custom fields by default:
(ref: https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py#L227)
To go around it, simply assign the custom field values before doing user.save()
self.cleaned_data = self.get_cleaned_data()
adapter.save_user(request, user, self)
setup_user_email(request, user, [])
user.address = self.cleaned_data.get('address')
user.user_type = self.cleaned_data.get('user_type')
user.save()
return user
That was a dirty fix. A cleaner way would be to override the allauth adapter to support your custom fields.
To override the default adapter and save the custom fields try the following
Create an adapters.py file in your app root folder and paste the code below
from allauth.account.adapter import DefaultAccountAdapter
class CustomUserAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
"""
Saves a new `User` instance using information provided in the
signup form.
"""
from allauth.account.utils import user_field
user = super().save_user(request, user, form, False)
user_field(user, 'address', request.data.get('address', ''))
user_field(user, 'first_name', request.data.get('first_name', ''))
user_field(user, 'last_name', request.data.get('last_name', ''))
user_field(user, 'user_type', request.data.get('user_type', ''))
user.save()
return user
Lastly set the settings configuration to use your custom adapter by adding this line in the settings.py file
ACCOUNT_ADAPTER = 'users.adapters.CustomUserAccountAdapter'