Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Getting to Know DRF

00:00 Django REST Framework Another popular option for building REST APIs is Django REST Framework, commonly known as DRF. Django REST Framework is a Django plugin that adds REST API functionality on top of a Django project, whether a new one that you create or an existing project with data present in it already.

00:21 This means that to use Django REST Framework, you need a Django project to work with. If you already have one, then you can apply the patterns in this section to your project.

00:30 Otherwise, follow along and you’ll build a Django project and add in Django REST Framework. First, install Django and Django REST Framework with pip as seen on screen into the virtual environment you created earlier on.

00:53 You can now use the django-admin tool to create a new Django project. Run the command seen on screen to start it.

01:04 This creates a new folder in your current directory called countryapi. Inside this folder are all the files you’ll need to run your Django project.

01:13 Next, you’ll create a new Django application inside the project. Django breaks up the functionality of projects into applications, and each application manages a distinct part of the project.

01:25 You’re only going to scratch the surface of what Django can do in this course. If you’re interested in learning more, then check out the available Real Python Django tutorials. To create the application, first change directories to countryapi and then run the manage.py command to create a new app.

01:48 This will create a new countries/ folder inside your project. Inside it are the base files for the application as you can see when you list its contents.

02:00 Now you’ve created an application to work with you’ll need to tell Django about it. Alongside the countries/ folder that you just created is another folder called country_api/.

02:10 This folder contains configurations and settings for your project as you can see when listing its contents.

02:20 Note that this folder has the same name as the root folder that Django created when you ran django-admin startproject countryapi.

02:29 Open up the settings.py file that’s inside the country api/ folder. Add two lines to INSTALLED_APPS to tell Django about Django REST Framework and the countries application.

02:44 You may be wondering why you need to add rest_framework into the applications list. You need to do this because Django REST Framework is just another Django application.

02:54 Django plugins are Django applications that are packaged up and distributed so that anyone can use them. The next step is to create a Django model to define the fields of your data.

03:05 Inside the countries application, update models.py with the code seen on screen.

03:24 This defines a Country model. Django will use this model to create the database table and columns for the country data. Run the command seen on screen to have Django update the database based on this model.

03:48 These commands use Django migrations to create a new table in the database. If you’d like to learn more about Django migrations, you can do that with this Real Python video course.

04:01 In the next section of the course, you’ll look at the steps needed to get your Django REST API working, loading and serializing data.

Become a Member to join the conversation.