If you make a get request to the “/auth/users/” endpoint, you will get an error that says that you cant do that. So you have to first create a user before.
Now if you look at the bottom of the api page, you will see some fields for you to enter the email, username and password of the user you wish to register. When you do that and send the post request, the user would’ve been created for you.
What if we want to include the First and Last names of our user while creating them. Note that the fields in the api come from a serializer, so Djoser has a serializer that serialized that data, so we need to create a custom serializer and add the fields we want. If you go to the Djoser docs you will see that the serializers in its settings. But now we want to create a custom serializer, where exactly do we create the custom serializer. this is where the “core” app comes into place. We created this app a few lessons ago, create a serializers.py file in that app and then…
from djoser.serializers import UserCreateSerializer as BaseUserCreateSerializer #we dont wanna cause a nameclash with the below class so we gave it an alias
class UserCreateSerializer(BaseUserCreateSerializer): #we want all the functionalities of the original CreateUserSerializer so we inherit from it class Meta(BaseUserCreateSerializer.Meta): fields = ['id', 'username', 'password', 'email', 'first_name', 'last_name'] # add the fields we want to be includedAnd then we have to register our serializer in our settings.py
DJOSER = { 'SERIALIZERS': { 'user_create': 'core.serializers.UserCreateSerializer' }}Now what if we want to include some profile info. like birthdate while registering the user. We can add the birth_date to the list of fields, but note that the birth_date isnt part of our user table, but rather our customer table. So part of our fields belong to the user table, and part of our fields belong to our profile info. How we will implement this is that when a user clicks a button to register, we will send a POST request to the user endpoint to create a user, and send a PUT request to the profile endpoint to update the profile information, and add a birthdate.