This is because django-rest-auth’s default register serializer only recognizes fields for django built-in user model. You will notice that the extra fields we provided in our custom user model (Citizen) are not added in the registration form. Now, open your browser and go to 127.0.0.1:8000/rest-auth/registration Run python manage.py runserver in your terminal. Path( 'rest-auth/registration/', include( 'rest_')), Path( 'rest-auth/', include( 'rest_auth.urls')), Your urls.py should look like this: # FIMA URL Configuration from ntrib import admin from django.urls import path, include In FIMA directory, find urls.py and add URL patterns for rest_auth. Now that we have our default user model set as Citizen. Python manage.py migrate THE BRASS TACKS: SERIALIZING CUSTOM USER MODEL You do this by executing the following commands in your terminal: Now, you need make migrations for Citizen model and create tables for your models (including models from installed packages declared in INSTALLED_APPS earlier). Now, set your Citizen model as the default user model for project FIMA.Īll you need to do is add AUTH_USER_MODEL in your settings.py as shown below: This means that aside from age, occupation, and date_of_birth fields, a Citizen instance will also have username, password, first_name, last_name, email fields. Our Citizen model will inherit all the basic django user fields from AbstractUser. Occupation = models.CharField(max_length= 50)ĭef _str_( self): return "%s %s" % (self.first_name, self.last_name) We will now create our custom user model as shown below: from django.db import modelsįrom import AbstractUser Now, go to models.py in citizens app folder. I want to believe you have all the settings properly configured in your settings.py as described before this section. Add the following to your settings.py: SITE_ID = 1ĪCCOUNT_AUTHENTICATION_METHOD = 'username' Now, you will need to include settings for django REST framework, rest_auth, and allauth. Your INSTALLED_APPS in settings.py should look like this: INSTALLED_APPS = [ Let's update INSTALLED_APPS in the settings.py located in FIMA directory. Now that you've installed the required packages for the REST API, created project FIMA and the citizens application. You do these by executing the corresponding commands in your terminal. Now, let's create our FIMA project and start a new application named 'citizens' for our project. This is optional but a good practice to manage all your project dependencies in one file. You can also add all your installed packages in requirements.txt by executing pip freeze > requirements.txt. Pip install django djangorestframework django-rest-auth django-allauth Install the packages you will be using for this tutorial by executing the command below in your terminal: Then create and activate a virtual environment to your project. Just follow the steps so that we can get to the real deal as soon as possible.įrom your terminal, change your working directory to your project folder. My sure guess right now is that you’re not new to Django if you’ve followed this post to this point (unless you’re planning to hire me). The brass tacks: Registering new user instance The brass tacks: Serializing custom user model Here is the table of content for this tutorial If you do not want to follow through this tutorial, feel free to jump right to any section you want. FIMA is an application that holds and manage information of citizens. In this tutorial, we will be using a user model for a simple Federal Identity Management Application (FIMA). We will take a tutorial approach to explain the process. Don’t worry! You’re about to learn the best approach to deal with this issue. Perhaps, you are facing this problem presently and that’s why you’re reading this now. One of the common headaches you’re likely to face when building a web API with Django REST Framework is how to register custom user instances. This means you will have to create a custom user model to fit your project requirements and still make use of the rich Django support for working with user instances. Most of the time, when you are building a real-world web application, Django's built-in user model will not be a perfect fit for your application because you might need to collect other vital user information that are not part of the built-in user model.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |