Hello! I’m quite new to django and I’m facing an issue.
I’m working on a web-based app that uses Django in the backend. We are trying to connect the app to several external APIs to fetch environmental data. They way this is currently done is like this: for each external API a new Django app is created and in each app views.py three django views are coded:
def get_stations(request) -> JSONResponse:
"""Fetch stations as points (lat and long) that can be plotted on the frontend"""
def get_parameters(request) -> JSONResponse:
"""Fetch the parameters available by station (this view is called after clicking on a station)"""
def get_timeseries(request) -> JSON Response:
"""Fetch the events from a specific parameter (called after clicking on a parameter)"""
Each of this views calls different functions that request the data from the associated API and then return a normalized response. For instance, the get_parameters view for the station ATK_01 of the API Example would return a List
of parameters that look like this:
[{"ID": "PCP00123", "Name": "Precipitation", "Location_ID": "ATK_01", "API_source": "EXAMPLE", "Units": "mm"}, {"ID": "SNOW0001", "Name": "Snow Depth", "Location_ID": "ATK_01", "API_source": "EXAMPLE", "Units": "mm"}]
And the urls.py of each app looks like this:
from django.urls import path
from .views import get_parameters, get_locations, get_timeseries,
urlpatterns = [
path("locations/", get_locations),
path("parameters/", get_parameters),
path("timeseries/", get_timeseries)
]
Now, this feels redundant. Also, I end up with lots of non-relevant files each time I start a new app (admin.py, models.py, tests.py, etc).
My question is what would be the smartest way to organize the application for a task like this? Also, how could I ensure that my responses are homogenous and well formated, other than typing the dictionary keys manually?
Thank you in advance.