Getting Started

Part 1, Chapter 3

In this chapter, we'll set up the base project structure.

Start by creating a new working directory, installing Django and Django REST Framework, and creating a new Django project and app:

$ mkdir django-tdd-docker && cd django-tdd-docker
$ mkdir app && cd app
$ python3.11 -m venv env
$ source env/bin/activate

(env)$ pip install django==4.1.7 djangorestframework==3.14.0
(env)$ django-admin startproject drf_project .
(env)$ python startapp movies

Feel free to swap out virtualenv and Pip for Poetry or Pipenv. For more, review Modern Python Environments.

Add the apps to the INSTALLED_APPS setting in the file:

# app/drf_project/

    'rest_framework', # new
    'movies', # new

Next, before applying the first migration, let's create a custom User model.

It's a good idea to configure a custom User model when starting a new Django project. For more, review Creating a Custom User Model in Django.

Add the following to the bottom of to make Django reference a User model of our design:

# app/drf_project/

AUTH_USER_MODEL = 'movies.CustomUser'

Next, define the custom User model in the movies/ file:

# app/movies/

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):

Here, we created a new class called CustomUser that subclasses AbstractUser.

That's it for now. Since we're not making any changes just yet to the User model, we won't make any updates to the Django admin app.

Go ahead and create the migration file and apply all the migrations to the database:

(env)$ python makemigrations
(env)$ python migrate

To verify that CustomUser is being used in place of User, you can view the schema within the SQLite shell:

$ sqlite3 db.sqlite3

SQLite version 3.39.5 2022-10-14 20:58:05
Enter ".help" for usage hints.

sqlite> .tables

auth_group                          django_migrations
auth_group_permissions              django_session
auth_permission                     movies_customuser
django_admin_log                    movies_customuser_groups
django_content_type                 movies_customuser_user_permissions

sqlite> .schema movies_customuser

CREATE TABLE IF NOT EXISTS "movies_customuser" (
  "password" varchar(128) NOT NULL,
  "last_login" datetime NULL,
  "is_superuser" bool NOT NULL,
  "username" varchar(150) NOT NULL UNIQUE,
  "first_name" varchar(30) NOT NULL,
  "last_name" varchar(150) NOT NULL,
  "email" varchar(254) NOT NULL,
  "is_staff" bool NOT NULL,
  "is_active" bool NOT NULL,
  "date_joined" datetime NOT NULL

sqlite> .exit

Create a new superuser and run the Django development server:

(env)$ python createsuperuser
(env)$ python runserver

Navigate to http://localhost:8000/ within your browser of choice to view the Django welcome screen. Make sure you can also log in to the Django admin at http://localhost:8000/admin/ with your superuser credentials. Kill the server once done. Exit then remove the virtual environment as well.

Then, add a requirements.txt file to the "app" directory:


Add a .gitignore to the project root:


Your directory structure should look like this:

├── .gitignore
└── app
    ├── db.sqlite3
    ├── drf_project
    │   ├──
    │   ├──
    │   ├──
    │   ├──
    │   └──
    ├── movies
    │   ├──
    │   ├──
    │   ├──
    │   ├── migrations
    │   │   ├──
    │   │   └──
    │   ├──
    │   ├──
    │   └──
    └── requirements.txt

Init a git repo and commit your code.

Mark as Completed