Create Task model

Follow the below steps to create the task model:

Step 1: In our API app in models.py file include the below code:

Python3




from django.db import models
  
class Task(models.Model):
    task_name = models.CharField(max_length=255)
    task_description = models.TextField()
    task_time = models.DateTimeField(auto_now_add=True)
    task_iscompleted = models.BooleanField()
  
    def __str__(self):
        return self.task_name


  1. Create a class and extended it with models.Model.
  2. Write different class fields which represent columns of our model.
  3. At last, we return task_name for verification. 

Step 2: Register Task Model.

  • Head over to admin.py and register the model using the following code:

Python3




from django.contrib import admin
from .models import Task
  
admin.site.register(Task)


Step 3: Migrating changes. 

  • It’s time to migrate our changes (make sure you migrate every time you change/create a model). First, make your migrations using 
$ docker-compose run app sh -c "python manage.py makemigrations"
  • Now Migrate.
docker-compose run app sh -c "python manage.py migrate"

Step 4: Create Model Serializer.

  • Create a serializer for our model in the API app create a file called serializers.py and include serializers are used to validate the incoming request data.

Python3




from rest_framework import serializers
from .models import Task
  
class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = '__all__'


Step 5: Test the task model.

  • Run the following command:
docker-compose run app sh -c "python manage.py test"
  • Make sure all the tests pass.
  • Now that we have our model created its time to write API views.

Python Django – Test Driven Development of Web API using DRF & Docker

We are going to create a to-do list web API using Django rest framework, docker and also going to write different tests for different functionalities in our code using test-driven development, but let’s first see what are prerequisites for this project.

Prerequisites :

  1. Docker Installed on your local system
  2. Basic knowledge of python 3.0
  3. Basic knowledge of Django 3.0

Now we are ready to go, let’s learn more about docker and Test-driven development (TDD) and why should we use them.

Docker :

Docker is an open-source containerization platform for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises.

Consider a situation where a software engineer writes a code and sends it for testing but that code won’t run on the tester’s local environment because all dependencies are not fulfilled, this problem can be eliminated simply by using docker.

Test-Driven Development (TDD):

Test-Driven Development is a software development practice where the focus is on writing unit tests before writing actual code, it is an iterative approach that combines programming, the creation of unit tests, and refactoring.

Unit Test: Unit tests are the test for testing different functionalities in our code

Three key steps for writing tests:

  1. Setup: creating sample functions that will be used in different test
  2. Execution: Calling the code which is being tested
  3. Assertion: comparing the results with expected results.

Now let’s move to the actual building part.

Similar Reads

Creating the project and Setting Up the Dockerfile:

Follow the below steps to create a project and set up the Dockerfile....

Rules for Writing Tests in Django:

...

Writing Tests for Testing task Model:

Following rules need to be followed while writing a Django test:...

Create Task model:

Follow the below steps to write the test for the task model:...

Writing test for API Views:

...

Writing API Views and URLs:

Follow the below steps to create the task model:...

Final Test:

...