Sitemap
Alchemist

Alchemist

Data Engineer, AI & ML Passionate And Life Entrepreneur

Creating a custom user model in Django / Rest API (step by step) — Part (1)

3 min readFeb 29, 2020

--

Press enter or click to view image in full size
Photo by Sarah Dorweiler on Unsplash

The Why ?

I use python for all my project, but when i start using it (Django) for my web application, i found that the default behavior of authentication module doesn't fit the requirement. (e.g email authentication)

In this post i will try to explain the steps to create a personalized User object in Django with the DRY in Mind (Don’t repeat yourself).

Before going further, this article is the result of search and tests i made based on great articles i found in the internet & books. so Thank You (all the authors)

What this post (series) about ?

  • Create CRUD application Using Django,
  • Extend the default user module,
  • Create Rest Interface (DRF) for the User Model,
  • Manage permissions for the new User,
  • Manage object level permissions,
  • Add Token authentication.
  • Listing all note in relation with a User,
  • Invite & share notes with other users.

I will try to make this as short as possible for three reasons:

  1. The Django & DRF Documentation are very detailed,
  2. I want to make this post as reference for quickstart,
  3. I’m not that good in English :D

Goal : Create our Django project and start first app.

We will create a project with the name sharenote, it will be our learning project.

Get Alchemist’s stories in your inbox

Join Medium for free to get updates from this writer.

Quick description: sharenote is a web app that allow user to create and store notes, later the user will be able to invite other user to join his workspace and view his notes.

Requirements : what you need.

  • Python knowledge (obviously)
  • Django knowledge
  • Python 3+ With pip utility or (Conda for python 3+)
  • Postgresql or Mysql instance ( actually for the purpose of this project we can use the lightweight database SQLLite (included in python) which is a good database for micro project or development mode, i always prefer a production ready environment , and in this tutorial we will try to make an ready to go application)
  • Your favorite Code Editors. (vscode)
  • 1 Cup of Coffee.

GTD : Get Things Done

Finally …

  1. Prepare the environment

Let’s start with creating a python virtual environment, it’s a best practice to isolate libraries in relation with project and avoid global installs. (learn more about virtual env)

  • Install virtualenv utility:

Open your favorite command line tool:

$ pip install virtualenv
  • Create virtual environment:
$ mkdir sharenote & cd sharenotes$ virtualenv sharenote_env
  • Activate your fresh environment:
$ source venv/bin/activate
  • Install Django:

I use the latest Django version (3.0.3)

$ pip install django

2. Create django project

  • Start new django project

Django provide great tools to interact with the library.

$ django-admin startproject sharenote

This command will create a new Django project folder with the given name, i will not go through project structure description etc, the official django documentation is really complete.

3. Create profile application

let’s create our new application inside the django project, this is another feature of django. Applications give your project a modulable structure so you can reuse the app in other project. Don’t repeat yourself

Now we will start a new application with the name profile, this app will be used to store the new User Model and extend it with new features.

$ cd sharenote$ python manage.py startapp profile

Ok, Test time.

The next command will start the django dev server:

$ python manage.py runserver

You should be able to access Django welcome page on : http://127.0.0.1:8000/

Next ?

In the next article in this series, We will extend the user model and take a look on the database settings.

--

--