visit
But if you feel confident in yourself, let us ride along. These are links to the first and second articles.
code . #Take note of the period
# Create a workspace inside the project workspace
$ touch tribal.code-workspace # For VScode environment setup
# Create the requirements.txt file and list all the dependencies of the project
# This makes it possible to install the listed dependencies at once.
$ touch requirements.txt
# Inside requirements.txt enter following dependencies
django>=3.2.0,<4.0
djangorestframework
pyyaml
requests
django-cos-headers
psycopg2-binary
# Upgrade your pip installer
$ pip install --upgrade pip
# Install the dependencies
$ pip install -r requirements.txt
$ sudo -u postgres psql
# Create a new database at postgresql shell
postgres=# create database tribalapi;
# Create a user with and encrypted password
# Choose appropriate namem and password
postgres=# create user tribal with encrypted password 'password123';
# Now let's grant the new user all privileges to our database
postgres=# grant all privileges on database tribalapi to tribal;
# Create Django project
$ django-admin start project tribalproject . # Don't forget to add the period
# Create Djanog app using python
$ python manage.py start triabalapp # Do not include a period
# Inside settingd.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'tribal_app',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password123',
'HOST': 'localhost',
'PORT': '5432',
}
}
from django.db import models
#Create your models here.
# This model translates our Regions table in the database
class Region(models.Model):
name = models.CharField(max_length=255)
country = models.ForeignKey('Country', on_delete=models.CASCADE)
def __str__(self):
return self.name
# This model translates our Countries table in the database
class Country(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
def __str__(self):
return self.name
# This model translates our Tribes table in the database
class Tribe(models.Model):
name = models.CharField(max_length=255)
location = models.CharField(max_length=255)
description = models.TextField()
region = models.ForeignKey(Region, on_delete=models.CASCADE)
def __str__(self):
return self.name
# This model translates our Traditonal Healing table in the database
class HealingPractice(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
region = models.ForeignKey(Region, on_delete=models.CASCADE)
def __str__(self):
return self.name
# This model translates our Demorgraphy table in the database
class Demography(models.Model):
tribe = models.ForeignKey(Tribe, on_delete=models.CASCADE)
population = models.IntegerField()
language = models.CharField(max_length=255)
religion = models.CharField(max_length=255)
def __str__(self):
return f"{self.tribe} Demography"
# This model translates our Economic Activity table in the database
class EconomicActivity(models.Model):
tribe = models.ForeignKey(Tribe, on_delete=models.CASCADE)
activity = models.CharField(max_length=255)
trade = models.CharField(max_length=255)
description = models.TextField()
def __str__(self):
return f"{self.tribe} Economic Activity"
# This model translates our Environment table in the database
class Environment(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
region = models.ForeignKey(Region, on_delete=models.CASCADE)
def __str__(self):
return self.name
# This model translates our Traditional Authority table in the database
class TraditionalAuthority(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
tribe = models.ForeignKey(Tribe, on_delete=models.CASCADE)
def __str__(self):
return f"{self.tribe} Traditional Authority"
# This model translates our TribesRegion table in the database
class TribeRegion(models.Model):
tribe = models.ForeignKey(Tribe, on_delete=models.CASCADE)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
class Meta:
unique_together = ('tribe', 'region')
def __str__(self):
return f"{self.tribe} - {self.region}"
A __str__
method is defined for each model to help with debugging and viewing and returns a string representation of the model instance. This is used when displaying the object in the Django admin UI or other parts of the app.
Note that I added aunique_together
constraint to the TribeRegion model metaclass, so that each tribe can only be associated with a region once.
from rest_framework import serializers
from .models import *
# This class serializes the Region model into JSON for easy transfere
class RegionSerializer(serializers.ModelSerializer):
class Meta:
model = Region
fields = '__all__'
# This class serializes the Country model into JSON for easy transfere
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = '__all__'
# This class serializes the Tribes model into JSON for easy transfere
class TribeSerializer(serializers.ModelSerializer):
region = RegionSerializer()
class Meta:
model = Tribe
fields = '__all__'
# This class serializes the Traditional Healing model into JSON for easy transfere
class HealingPracticeSerializer(serializers.ModelSerializer):
region = RegionSerializer()
class Meta:
model = HealingPractice
fields = '__all__'
# This class serializes the Demography model into JSON for easy transfere
class DemographySerializer(serializers.ModelSerializer):
tribe = TribeSerializer()
class Meta:
model = Demography
fields = '__all__'
# This class serializes the Economic Activity model into JSON for easy transfere
class EconomicActivitySerializer(serializers.ModelSerializer):
tribe = TribeSerializer()
class Meta:
model = EconomicActivity
fields = '__all__'
# This class serializes the Environment model into JSON for easy transfere
class EnvironmentSerializer(serializers.ModelSerializer):
region = RegionSerializer()
class Meta:
model = Environment
fields = '__all__'
# This class serializes the Tradional Authority model into JSON for easy transfere
class TraditionalAuthoritySerializer(serializers.ModelSerializer):
tribe = TribeSerializer()
class Meta:
model = TraditionalAuthority
fields = '__all__'
# This class serializes the TribeRegion model into JSON for easy transfere
class TribeRegionSerializer(serializers.ModelSerializer):
tribe = TribeSerializer()
region = RegionSerializer()
class Meta:
model = TribeRegion
fields = '__all__'
$ python manage.py makemigrations
$ python manage.py migrate