Loading and Serializing Data With DRF
00:00 Loading and Serializing Data You’ve just used Django’s migrations to create your database tables, but the table will start out empty. It would be nice to have some initial data so that you can test Django REST framework straight away by using endpoints to view that data.
00:16 To do this, you are going to use a Django fixture to load some data into the database. Once you’ve completed this course, if you realize that Django fixtures will be useful for you, then Real Python has you covered with this full tutorial.
00:29 Covering the subject in much more depth.
00:33
The JSON data that you’ll need is seen on screen, but to save you typing it, you’ll find countries.json
in the course materials. Make sure it’s copied into the countries/
directory.
00:44 The JSON contains database entries for three countries. You can load them into the database using this command.
00:56 This will add three rows to the database, and with that, your Django application is all set up and populated with some data. You can now start adding Django REST Framework to the project.
01:08
Django REST framework takes an existing Django model and converts it to JSON for a REST API. It does this with ModelSerializers
. A ModelSerializer
tells Django REST framework how to convert a model instance into JSON and what data to include.
01:24
You’ll create your serializer for the Country
model you created earlier.
01:29
Create a file called serializers.py
inside the countries/
folder. Then add the code seen on screen.
01:47
This serializer CountrySerializer
subclasses serializers
.ModelSerializer
to automatically generate JSON content based on the model fields of Country
.
01:59
Unless specified, a ModelSerializer
subclass will include all fields from the Django model in the JSON. You can modify this behavior by setting fields
to a list of data you wish to include.
02:19
Just like Django, Django REST framework uses views to query data from the database to display to the user. Instead of writing REST API views from scratch, you can subclass Django REST framework’s ModelViewSet
class, which has default views for common REST API operations.
02:36
Note that the Django REST framework documentation refers to these views as actions. Here’s a list of the actions that ModelViewSet
provides and their equivalent HTTP methods.
02:48 As you can see, these actions map to the standard HTTP methods you’d expect in a REST API. You can override these actions in your subclass or add additional actions based on the requirements of your API.
03:01
Next up is the code for a ModelViewSet
subclass called CountryViewSet
. This class will generate the views needed to manage Country
data.
03:11
Add the following code to views.py
inside the countries/
application.
03:42
In this class, serializer_class
is set to CountrySerializer
and the queryset
is set to Country.objects
.all()
. This tells Django REST framework which serializer to use and how to query the database for this specific set of views.
03:58 Once the views are created, they need to be mapped to the appropriate URLs or endpoints, and you’ll be doing that and using the API in the next part of the course.
Become a Member to join the conversation.