In the previous tutorial, we learned how to combine Django with FastAPI to build a blog covering all aspects of a CRUD project.
We created the models and ORM in Django and a CRUD API Endpoint with FastAPI.
We also set up: validations using pydantic, Django ASGI and WSGI to work together with FastAPI, and a basic API versioning with FastAPI.
In this tutorial, you will learn to create a User Model with FastAPI and Django.
The codes for this tutorial build upon the previous one.
Step 1: Create a Django Accounts App
-
How to Create a Django Accounts App
-
Add accounts to Installed App in Django Settings
Step 2: Create a Django User Model and Manager
-
How to Create a Django User Model with Fullname Field and Email
The code below will create a User Model Field with a
fullname
andemail
as a username. -
How to Create a Django Manager for a User Model
Create a
manager.py
file in theaccounts
app and add the codes below. -
How to Add an Admin for a Django User Model
-
Add the helper functions below to the
utils.py
file in the core module
Step 3: Migrate the Models
Run the commands below to migrate our user model's data.
Step 4: Create a Super User
Run the command below to create a superuser for our app.
If you successfully created a superuser, run the django server and log in to the admin page to test our models.
Wrap Off
In this tutorial, we focused on Django mainly to add a user model to the project.
Next, we will improve the accounts app to be production-ready. We will use Django Rest Framework and Django packages like Django AllAuth, dj-rest-auth for authentication and authorization.