![]() Router.register(r'people', PersonViewSet) from django.urls import include, pathįrom my_awesome_api.views import PersonViewSet, SpeciesViewSet Let’s create a new file my_awesome_api/urls.py and add the router configuration as shown below. from rest_framework import viewsetsįrom my_awesome_api.serializers import PersonSerializer, SpeciesSerializerĬlass PersonViewSet(viewsets.ModelViewSet):Ĭlass SpeciesViewSet(viewsets.ModelViewSet):Īfter the viewsets are defined we can now use the router functionality provided by DRF to route a desired API endpoint to the given viewset. Viewsets provide the advantage of combining multiple sets of logic into a single class. Let’s start by adding 2 viewsets for each of the models we created in a new file my_awesome_api/views.py. from rest_framework import serializersįrom my_awesome_api.models import Person, SpeciesĬlass PersonSerializer(serializers.ModelSerializer):įields = ('name', 'birth_year', 'eye_color', 'species')Ĭlass SpeciesSerializer(serializers.ModelSerializer):įields = ('name', 'classification', 'language') Set Up Routers and Create API URLsĪfter the serializers are created we need to create a view to the API and connect it to the Django URLs. We will add the serializers by creating a new file my_awesome_api/serializers.py. The serializers will convert the Person model and Species model into JSON that will be used by the API to return the data to the user. Now that we have added the models and created the tables, it is time to tell DRF how to serialize the models. (drf) $ python3 manage.py migrateĪpplying my_awesome_api.0001_initial. My_awesome_api/migrations/0001_initial.pyĪfter the migrations files are created, we can run the migrate command. The first step is to create a new migration by running the makemigrations command. Species = models.ForeignKey(Species, on_delete=models.DO_NOTHING)Īfter the models have been added, let’s run the migrations to let Django know that we are going to add 2 new tables to the database. Language = models.CharField(max_length=100)īirth_year = models.CharField(max_length=10)Įye_color = models.CharField(max_length=10) from django.db import modelsĬlassification = models.CharField(max_length=100) Let’s start by adding Person and Species into my_awesome_api/models.py. If everything works as shown above, and if we go to in the browser we should see the following success message.Īt this point we will be mostly editing the files in the my_awesome_api directory. (drf) $ python3 manage.py runserverĭjango version 3.1.5, using settings 'my_awesome_django_ttings' Let’s make sure the app is up and running by using the Django runserver command. We should also add rest_api_framework to this list. Manage.py my_awesome_api my_awesome_django_projectĪfter the app is created, let’s register the app by adding the path to the app config in the my_awesome_django_project/settings.py. (drf) $ django-admin startapp my_awesome_api (drf) $ django-admin startproject my_awesome_django_project Successfully installed djangorestframework-3.12.2Īfter installing the necessary requirements let’s create a Django project and app. $ pyenv virtualenv drfĬreated virtual environment CPython3.6.4.final.0-64 in 400msĪfter the virtual environment is created and activated, let’s install django and djangorestframework which are the necessary Python libraries. For this example I used pyenv to manage my virtual environment. Let’s start by creating a Python virtual environment and activating it in the desired working directory. There are 5 total steps to have our REST API fully working: At the end of the tutorial we should have the following 4 API endpoints up and running: We will be assuming you are already familiar with Django models/migrations and REST. In this tutorial we are going to look at how to use DRF to create a very simple Star Wars themed REST API. DRF complements Django by providing a means of converting it’s models to a REST-ful format. While Django does a lot of heavy lifting by connecting it’s models to the database, serializing those models into a JSON format is still a big challenge. The reason I love Django Rest Framework (DRF) is because it elegantly handles data serialization and seamlessly works with Django’s ORM.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |