How to seed a Django API with data from an external API

Alexis Chilinski
2 min readJan 28, 2020

--

Since I started at Flatiron School, one of my main goals has been to learn Python and its frameworks. Obviously a main reason is because it’s named after Monty Python, but I’m also well aware of its widespread usage and the fact that it’s a relatively easy-to-learn language for Ruby developers (such as myself).

Python syntax was easy enough to pick up, but I wanted to build a workable backend with it. I’d heard about both Flask and Django, but based on information from a number of resources, Django seemed like the framework that was best suited for what I needed to build — a more complex web application.

While the docs on Django are plentiful and it has plenty of Stack Overflow questions, I could not for the life of me find a definitive answer on how to seed my database with data from an external API. So I’d like to provide the solution I found to help future devs.

You can create a custom seed command, but the files have to be added manually. Within your app file, create a management directory with a commands directory inside. Create a seed.py file inside /management/commands/.

At the top of the seed.py file, you need to import requests (the library to retrieve information from a website, you also have to run pip install requests. ). You must also import the BaseCommand class (from the library that holds Django’s built-in commands), and of course your models that require data (in my case, Fish).

import requests
from django.core.management.base import BaseCommand
from ...models import Fish

Next, build the functions that get and distribute the API data. For my project, I used a sustainable seafood API (https://www.fishwatch.gov/developers). First you need to extract the data, which is where requests comes in.

def get_fishs():
url = 'https://www.fishwatch.gov/api/species'
r = requests.get(url, headers={'Content-Type':
'application/json'})
fish = r.json()
return fish

Once you have this get function working, you can use the return data to seed your database, and of course make sure the attributes you’re seeding match the respective model.

def seed_fish():
for i in get_fishs():
fish = Fish(
name=i["Species Name"],
scientific_name=i["Scientific Name"],
)
fish.save()

You can even include a clear_data function to delete all your seeds if you need to seed more than once (and avoid duplicates).

def clear_data():
Fish.objects.all().delete()

The last bit is to add the Command class that extends the BaseCommand class. This is what will call your custom functions. You can also include a print at the end to indicate when the command is successfully completed.

class Command(BaseCommand):
def handle(self, *args, **options):
seed_fish()
# clear_data()
print("completed")

Then, all you need to do is run python manage.py seed in the command line (custom commands will have the same name as the file you created in /management/commands/). Assuming your functions, imports, and classes are set up properly, you will have seeded data! To check if it worked, you can open the python manage.py shell and type in Fish.objects.all() (or whatever your model is).

Hope this saves someone a lot of time! Happy coding!

--

--