Skip to main content

Administrative Division

The administrative division module is a common API module used for remote data collection that provides information about geographical boundaries, such as countries, states, and municipalities. This module is particularly important for the output of the Remote Data Collection (e.g web-app that involve maps visualization and data filtering). By using the administrative division module, we can easily filter data by location and create interactive maps that highlight specific regions of interest. 

One common use case is for creating a cascading dropdown menu or hierarchical form, where the options available in one dropdown depend on the selection made in the previous dropdown. For example, if a user selects "Indonesia" in the first dropdown, the second dropdown would populate with the province in the "Indonesia", and so on.

In short, the administrative division module is a crucial building block for many different types of remote data collection applications, and its accurate and up-to-date information can improve the quality and usefulness of many other API modules that rely on location-based data.


Models

The database schema you provided has three fields: id, parent_id, and name. The id field represents the unique identifier for each administrative division, while the parent_id field represents the ID of the parent division (if applicable). Finally, the name field stores the name of the division.

Here's an example of how you might define a Django model to represent this schema:

from django.db import models

class Administration(models.Model):
    id = models.IntegerField(primary_key=True)
    parent_id = models.IntegerField(null=True, blank=True)
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

In this example, we define a Administration model with three fields: id, parent_id, and name. The id field is defined as an IntegerField with primary_key=True, which indicates that it is the primary key for the model. The parent_id field is also defined as an IntegerField, but with null=True and blank=True, which means that it can be empty or null. Finally, the name field is defined as a CharField with max_length=255, which limits the length of the name to 255 characters.

By defining this model, we can easily query and manipulate administrative divisions in our Django application. For example, we might use the following code to retrieve all administrative divisions with a parent ID of 42:

parent_division = Administration.objects.get(id=42)
child_divisions = Administration.objects.filter(parent_id=parent_division.id)

This would retrieve all administrative divisions that have 42 as their parent ID and allow us to perform further processing.


API Endpoint

Here's an example of how we might define our API endpoints using the Django REST Framework:

from rest_framework import serializers, viewsets, routers
from .models import Administration

class AdministrationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Administration
        fields = ('id', 'parent_id', 'name')

class AdministrationViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Administration.objects.all()
    serializer_class = AdministrationSerializer

router = routers.DefaultRouter()
router.register(r'administration', AdministrationViewSet)

In this example, we define an API endpoint for administrative divisions using the AdministrationSerializer and AdministrationViewSet classes. The AdministrationSerializer class specifies which fields from the AdministrativeDivision model we want to include in our API response, while the AdministrationViewSet class defines how we want to retrieve and display that data.