DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Creating a Countries API in Django with MySQL Database

Introduction
In this tutorial, we will walk you through the process of building a Countries API using Django, a powerful Python web framework. We will use MySQL as the database to store the information about countries and their attributes. Additionally, we'll enable searching for countries by their id.

Prerequisites:

Let's get started!

Step 1: Set up a virtual environment and install Django
Open your terminal or command prompt and create a new directory for your project:

mkdir countries_api
cd countries_api
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment and activate it:

python3 -m venv env

# For Windows:
. env/Scripts/activate

# For macOS/Linux:
source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install Django and Django REST framework:

pip install django djangorestframework mysqlclient
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a new Django project and app

Create a new Django project and navigate into it:

django-admin startproject countries_api .
Enter fullscreen mode Exit fullscreen mode

(Note the dot . at the end, which specifies the current directory as the project root.)

Create a new Django app within the project:

python manage.py startapp countries
Enter fullscreen mode Exit fullscreen mode

Step 3: Define the Country model

Open the countries/models.py file and define the Country model:

from django.db import models

class Country(models.Model):
    # Define the fields for the Country model
    name = models.CharField(max_length=100)
    iso3 = models.CharField(max_length=3, null=True)
    numeric_code = models.CharField(max_length=3, null=True)
    iso2 = models.CharField(max_length=2, null=True)
    phonecode = models.CharField(max_length=255, null=True)
    capital = models.CharField(max_length=255, null=True)
    currency = models.CharField(max_length=255, null=True)
    currency_name = models.CharField(max_length=255, null=True)
    currency_symbol = models.CharField(max_length=255, null=True)
    tld = models.CharField(max_length=255, null=True)
    native = models.CharField(max_length=255, null=True)
    region = models.CharField(max_length=255, null=True)
    subregion = models.CharField(max_length=255, null=True)
    timezones = models.TextField()
    translations = models.TextField()
    latitude = models.DecimalField(max_digits=10, decimal_places=8, null=True)
    longitude = models.DecimalField(max_digits=11, decimal_places=8, null=True)
    emoji = models.CharField(max_length=191, null=True)
    emojiU = models.CharField(max_length=191, null=True)
    created_at = models.DateTimeField(null=True)
    updated_at = models.DateTimeField(auto_now=True)
    flag = models.BooleanField(default=True)
    wikiDataId = models.CharField(max_length=255, null=True, db_column='wikiDataId', db_comment='Rapid API GeoDB Cities')

    def __str__(self):
        return self.name
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the MySQL database and run migrations

Ensure your MySQL server is running. Then, open the countries_api/settings.py file and update the DATABASES settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_mysql_db_name',
        'USER': 'your_mysql_username',
        'PASSWORD': 'your_mysql_password',
        'HOST': 'your_mysql_host',  # Use 'localhost' if the database is on the same machine
        'PORT': 'your_mysql_port',  # Typically 3306 for MySQL
    }
}
Enter fullscreen mode Exit fullscreen mode

Replace 'your_mysql_db_name', 'your_mysql_username', 'your_mysql_password', 'your_mysql_host', and 'your_mysql_port' with your actual MySQL database credentials.

Run the migrations to create the database tables:

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the API views and serializers

Open the countries/views.py file and define the API views:

from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import Country
from .serializers import CountrySerializer

class CountryViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Country.objects.all()
    serializer_class = CountrySerializer

    @action(detail=True, methods=['GET'])
    def search_by_id(self, request, pk=None):
        try:
            country = Country.objects.get(pk=pk)
            serializer = CountrySerializer(country)
            return Response(serializer.data)
        except Country.DoesNotExist:
            return Response({'error': 'Country not found'}, status=404)
Enter fullscreen mode Exit fullscreen mode

Step 6: Register the app and REST framework in settings

Open the countries_api/settings.py file and update the INSTALLED_APPS and REST_FRAMEWORK settings:

# countries_api/settings.py

# ...

INSTALLED_APPS = [
    # ...
    'rest_framework',
    'countries',
    # ...
]

# ...

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ],
    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
    ],
}
Enter fullscreen mode Exit fullscreen mode

Step 7: Configure the URLs

Open the countries_api/urls.py file and configure the URLs for the API views:

# countries_api/urls.py

from django.urls import include, path
from rest_framework import routers
from countries.views import CountryViewSet

router = routers.DefaultRouter()
router.register(r'countries', CountryViewSet)

urlpatterns = [
    path('', include(router.urls)),
]
Enter fullscreen mode Exit fullscreen mode

Step 8: Run the development server

Finally, run the development server:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Now your Countries API is up and running! You can access it at http://localhost:8000/countries/. The API supports basic CRUD operations for the Country model and allows you to search for countries by their id using the /countries/search_by_id/<id>/ endpoint.

Github link: https://github.com/sosmongare/countries_api

Conclusion:

In this tutorial, we have learned how to create a Countries API using Django with MySQL as the database. We have also enabled searching for countries by their id, making the API more powerful and flexible. By registering the app and the REST framework, we ensure that the API is properly integrated into the Django project.

Django provides a robust platform to build APIs, and MySQL offers a reliable database backend for storing your data. I hope you found this tutorial helpful in building your API. Incase of any assistance, kindly comment down.

Happy coding!

Top comments (2)

Collapse
 
ldnovaes profile image
Leandro Duarte

Great! The only thing I would correct is that, for Windows, the correct command to activate the virtual environment is .\env\Scripts\activate.

Collapse
 
msnmongare profile image
Sospeter Mong'are

Thank you @ldnovaes for pointing this out. Mine works work well, however, when I try to use yours, I am getting an error:

Terminal Output