How to save extra fields on registration using custom user model in DRF + django-rest-auth

限于喜欢 提交于 2019-12-03 08:58:21

I went looking for an answer myself. Spend some time digging in the source code. I realize this solution may be missing the actual validation for the extra fields added to the custom user model, but I will look into that later.

What I have written below I wrote with a potential blog post in mind.

I am going to assume you know how to set up a DRF project and install the above packages. The django-rest-auth documentation is clear on how to install that package (https://django-rest-auth.readthedocs.io/en/latest/index.html), make sure to also follow the steps to install the part of django-rest-auth for user registration.

Create a new app ‘users’

This app will hold my custom code for implementing the custom user model. I also install it in the Django main settings file:

settings.py:

INSTALLED_APPS = [
    ...
    'users',
]

Create my custom user model

Notice that I just added one custom field, but you can add whatever fields you want ofcourse.

users.models.py:

from django.contrib.auth.models import AbstractUser
from django.db import models


class CustomUser(AbstractUser):
    preferred_locale = models.CharField(max_length=2, blank=True, null=True)

Tell django to use the CustomUser model

settings.py:

…
AUTH_USER_MODEL = 'users.CustomUser'

Register Custom user model at Django admin

users.admin.py:

from django.contrib import admin

from .models import CustomUser


admin.site.register(CustomUser)

Make migrations and run them

This is the first time I do this for this project.

In command line:

python manage.py makemigrations users
python manage.py migrate

Registering new users with extra fields

If you start the Django development server now, you’ll see in the admin that you can see the custom user model, with the extra fields.

But when you go to ‘http://127.0.0.1:8000/rest-auth/registration/’ you don’t see the extra fields yet.

In the process of user registration two important classes are used, namely:

  • a serializer ‘rest_auth.registration.RegisterSerializer’
  • an adapter ‘allauth.account.adapter.DefaultAccountAdapter’

We’ll make a custom version of both of these, inheriting all the functionality of it’s parent class.

Creating a custom RegisterSerializer

Create a new file ‘serializers.py’ in the users app/folder.

users.serializers.py:

from rest_framework import serializers

from allauth.account.adapter import get_adapter
from allauth.account.utils import setup_user_email

from rest_auth.registration.serializers import RegisterSerializer


class CustomRegisterSerializer(RegisterSerializer):
    preferred_locale = serializers.CharField(
        required=False,
        max_length=2,
    )

    def get_cleaned_data(self):
        data_dict = super().get_cleaned_data()
        data_dict['preferred_locale'] = self.validated_data.get('preferred_locale', '')
        return data_dict

Here I create a new field for each extra field on the custom user model. So in my case a added this:

preferred_locale = serializers.CharField(
        required=False,
        max_length=2,
    )

Also, the get_cleaned_data method should return a dict which contains all the data for the fields that you want to have saved when registering a new user.

This is what the original method (of the default RegisterSerializer looks like):

def get_cleaned_data(self):
    return {
        'username': self.validated_data.get('username', ''),
        'password1': self.validated_data.get('password1', ''),
        'email': self.validated_data.get('email', '')
    }

As you can see it returns a dictionary, containing all the data for the new user. You want to add a keyval entry to this dictionary for each extra field you have added to your custom user model.

In my case, needing only to add data for the field ‘preferred_locale’, this is the resulting method:

def get_cleaned_data(self):
    data_dict = super().get_cleaned_data()
    data_dict['preferred_locale'] = self.validated_data.get('preferred_locale', '')
    return data_dict

Tell django to use this new serializer

settings.py:

REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'users.serializers.CustomRegisterSerializer',
}

Preventing errors

If you will try to register a new user, you might get the following error in the console where your development server is running: ConnectionRefusedError: [Errno 111] Connection refused

Although a user is still created, you can fix this error by adding the following line to your settings.py file:

settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Another error that will occur when you delete a user is:

django.db.utils.OperationalError: no such table: allauth_socialaccount

To solve this, add this to your settings.py:

settings.py:

INSTALLED_APPS = [
    ...
    'allauth.socialaccount',  
]

After that, you should apply migrations before you can continue:

python manage.py migrate

Creating a custom AccountAdapter

After the above steps, going to ‘http://127.0.0.1:8000/rest-auth/registration/’ will show you the extra fields. But when you register a new user, and send the data for the extra fields, the extra field’s data is not saved.

The last thing we need to do to solve this is to create a custom AccountAdapter

In our users app/folder create a new file named ‘adapter.py’:

users.adapter.py:

from allauth.account.adapter import DefaultAccountAdapter


class CustomAccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=False):
        user = super().save_user(request, user, form, commit)
        data = form.cleaned_data
        user.preferred_locale = data.get('preferred_locale')
        user.save()
        return user

Here, if you have followed the above steps correctly, you can access the data of the extra added fields in the form.cleaned_data dictionary. This is the dictionary that is returned by the get_cleaned_data method from our custom RegisterSerializer.

In the save_user method above, we can then use this data and save it to the appropriate fields, like so:

user.preferred_locale = data.get('preferred_locale')

Tell Django to use this new adapters

settings.py:

ACCOUNT_ADAPTER = 'users.adapter.CustomAccountAdapter'

Now you can register your user, using the django-rest-auth registration endpoint '/rest-auth/registration/', and send the data for the extra fields you added. This will all be saved in one request.

Again, I realize that custom validation for each field needs to be added. But that's another topic that I will dive into later, and update the post when I found out how that works precisely.

Let's break your question. Please note that I am explaining the basics of Django REST Framework to you.

Overriding User's Model

  • [x] Step 1: You override User model: You did this. Alternative? Yes. Create a model that has OneToOneForeignKey pointed to User model.
  • [x] Step 2: Use this CustomUserModel. To do this, you need to set AUTH_USER_MODEL in settings.py Link to official documentation You did this.
  • [ ] Step 3: Create a UserManager to handle user's registration and other information. You haven't done this.

Registration from API

  • [ ] Create a serializer that clearly mentions all the required fields that you're expecting from an end user. You can even use serializer.ModelSerializer if there are no custom fields.
  • [ ] Handle explicit verifications in serializer itself. Use def validate(self, attrs) if required. Here is the official document link.
  • [ ] Finally, create a view and use APIView as you will want to register a user using UserManager that you created above.

I can also refer to you an app that I built myself. Here's the link: DRF-USER. I customized User model to some extent and followed the same process.

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!