This project is a Django application set up to use multi-tenancy with django-tenants
and PostgreSQL. It includes three Django apps: schools
, students
, and dashboard
.
Follow these steps to set up your development environment and run the project.
python -m venv venv
On Windows
venv\Scripts\activate
On macOS/Linux:
source venv/bin/activate
pip install Django
pip install django-tenants
pip install psycopg2
pip install python-decouple
Generate the requirements.txt file:
pip freeze > requirements.txt
Create the Django project and apps:
django-admin startproject tenant_project .
django-admin startapp schools
django-admin startapp students
django-admin startapp dashboard
Follow the setup instructions for django-tenants
here.
Follow the PostgreSQL setup guide here.
-
Create a PostgreSQL Database
-
Use a PostgreSQL client or command-line tools to create a new database.
-
Example using
psql
command-line tool:psql -U postgres CREATE DATABASE mydatabase;
-
-
Configure Database Settings
-
Open
settings.py
and configure theDATABASES
setting to usedjango-tenants
with PostgreSQL. -
Add the following configuration:
DATABASES = { 'default': { 'ENGINE': 'django_tenants.postgresql_backend', 'NAME': config('DB_NAME'), 'USER': config('DB_USER'), 'PASSWORD': config('DB_PASSWORD'), 'HOST': config('DB_HOST'), 'PORT': config('DB_PORT'), } }
-
-
Add Environment Variables
-
Create a
.env
file in the root directory of your project to store sensitive information. -
Add the following environment variables to the
.env
file:DB_NAME=mydatabase DB_USER=myuser DB_PASSWORD=mypassword DB_HOST=localhost DB_PORT=5432
-
Install
python-decouple
if you haven’t already, to manage environment variables:pip install python-decouple
-
Ensure you have the following in your
settings.py
to read from the.env
file:from decouple import config
-
Create and apply initial migrations:
python manage.py makemigrations
python manage.py migrate_schemas --shared
Ensure that the database is empty when running the migrate_schemas --shared
command for the first time.
Create a public tenant and a new tenant using Django's shell:
python manage.py shell
In the shell:
from schools.models import Client, Domain
# Create public tenant
tenant = Client(schema_name='public', name='Public')
tenant.save()
# Add domain for public tenant
domain = Domain(domain='localhost', tenant=tenant, is_primary=True)
domain.save()
# Create a tenant for a new client
tenant = Client(schema_name='maplehigh', name='Maple High School')
tenant.save()
domain = Domain(domain='maplehigh.localhost', tenant=tenant, is_primary=True)
domain.save()
exit()
python manage.py runserver
In settings.py, add the following to use the schools URLs for the public schema:
PUBLIC_SCHEMA_URLCONF = 'schools.urls'
Create the Student model in the students app:
python manage.py makemigrations
python manage.py migrate_schemas
- For changes in models, always run
makemigrations
followed bymigrate_schemas --shared
. - Make sure to properly configure your PostgreSQL database and update your
.env
file accordingly.
These resources are useful for further reading and understanding the technologies used in this project.