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.12 -m venv env
$ source env/bin/activate

(env)$ pip install django==5.0.7 djangorestframework==3.15.2
(env)$ django-admin startproject drf_project .
(env)$ python manage.py 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 settings.py file:

# app/drf_project/settings.py

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "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 settings.py to make Django reference a User model of our design:

# app/drf_project/settings.py

AUTH_USER_MODEL = "movies.CustomUser"

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

# app/movies/models.py

from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    pass

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 manage.py makemigrations
(env)$ python manage.py 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" (
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  "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 manage.py createsuperuser
(env)$ python manage.py 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:

Django==5.0.7
djangorestframework==3.15.2

Add a .gitignore to the project root:

__pycache__
env
*.sqlite3

Your directory structure should look like this:

├── .gitignore
└── app
    ├── db.sqlite3
    ├── drf_project
    │   ├── __init__.py
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── manage.py
    ├── movies
    │   ├── __init__.py
    │   ├── admin.py
    │   ├── apps.py
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    └── requirements.txt

Init a git repo and commit your code.




Mark as Completed