Generating API Using Django Rest Framework With Insomnia
Generating API Using Django Rest Framework With Insomnia
T
his article will familiarise Pip can be defined as an installation Django project. To state bluntly, virtual
you with the basic structural manager for packages/libraries that are environments are environments that
layout and features that come written in Python. Let’s download a file can have distinct versions of packages/
with creating a Django project for API called get-pip.py: libraries installed within them,
generation. It will help you understand respectively. Let me explain that with
what models, serialisers and views curl https://bootstrap.pypa.io/get-pip. an example. We are in April 2022, and
are, how they are interconnected, py -o get-pip.py we intend to create a project (to be dealt
and how we can use Insomnia to test with in VS Code). Let’s say the latest
the functioning of CRUD (create, To install pip, give the following version of Python is 3.1 (which we have
read, update and delete) operations command: installed in our system and no virtual
on our APIs. environments have been created) having
python get-pip.py libraries a, b, c, d, e and f. Assume
Initial setup that our project requires libraries a, b
You will be required to use the and c, all of which are in Python 3.1;
Note: Figure 1 shows pip as
command prompt or carry out the initial hence our project will work without any
being uninstalled first and then
steps needed to create a Django project. reinstalled, because I already had hindrances.
Check the version of Python you have pip installed on my system. Now let’s say Python’s new version
installed on your system: 3.2 comes out a few weeks or a few
Now let us understand what ‘virtual months later which has libraries a,
python --version environments’ are before we create a d, e, f, g and h. We install this new
C:\Users\kamil\osfydir\
osfyproject1>workon venv1
(venv1) C:\Users\kamil\osfydir\
osfyproject1>
(venv1) C:\Users\kamil\osfydir\
osfyproject1>python manage.py startapp
firstapp
(venv1) C:\Users\kamil\osfydir\
osfyproject1>
Our idea is to create a simple Figure 6: Newly created database in pgAdmin (venv1) C:\Users\kamil\osfydir\
table/model taking in the information osfyproject1>python manage.py
of a student. The fields ‘First_name’ ‘default’: { makemigrations
and ‘Last_name’ are character fields, ‘ENGINE’: ‘django.db.backends. Migrations for ‘firstapp’:
which allow a maximum length of sqlite3’, firstapp\migrations\0001_initial.py
50 characters. The ‘Required=True’ ‘NAME’: BASE_DIR / ‘db. - Create model Studentinfo
parameter implies that you cannot sqlite3’,
discard this field (this will be covered in } The ‘makemigrations’ step
a case scenario later). The ‘null=False’ } automatically creates a file called
means that these fields will not accept ‘0001_initial.py’ in the migrations
null values. There are many different By default, it considers sqlite3 as folder within our ‘firstapp’. This file
types of fields and parameters for its database platform, but in our case, has code written in Python, which
Django models, which you can find in we are using PostgreSQL. The modified inculcates the definition of our
the official documentation of Django. section should appear like this: model in a way that PostgreSQL will
We now need to ‘send’ our defined understand. Let’s take a peek at how
model/table to our database. For DATABASES = { this looks:
this, let’s make a few changes to the ‘default’: {
‘DATABASES’ section within the ‘ENGINE’: ‘django.db.backends. from django.db import migrations,
settings.py file of ‘osfyproject1’ in VS postgresql’, models
Code. The pre-existing section looks as ‘NAME’: ‘osfyproject1’,
follows: ‘USER’:’postgres’, class Migration(migrations.Migration):
‘PASSWORD’:’password’, initial = True
DATABASES = { ‘HOST’:’localhost’, dependencies = [
urlpatterns = [ ---(5)
path(‘admin/’, admin.site.urls),
path(‘student_post_get/’,Student_
post_get_view.as_view()),
Figure 9: Sample error messages ]
def get(self,request,pk):
article=self.get_object(pk)
serializer=Studentserializer(a
rticle)
return Response(serializer.
data)
def put(self,request,pk):
article=self.get_object(pk)
serializer=Studentserializer(ar
ticle,data=request.data)
if serializer.is_valid(): Figure 13: Updated ‘GET’ output
serializer.save()
return Response(serializer. value. The ‘get’ function simply with pk=1. The change will be
data) displays the model entry with reflected when you ‘GET’ all
return Response(serializer. the given ‘pk’. ‘get’, ‘put’ and student info.
errors,status=status.HTTP_400_BAD_ ‘delete’ functions make use of the We will now delete the entry
REQUEST) ‘get_object’ function and treat it as in the model having pk=2. All
the first step in trying to obtain the you have to do is enter 2 in the
def delete(self,request,pk): required individual value. Add the URL as ‘http://127.0.0.1:8000/
article=self.get_object(pk) required URL in ‘urls.py’: student_details/2/’ and perform the
article.delete() ‘DELETE’ operation. Let’s see if the
return Response(status=status. path(“student_details/<int:pk>/”,Stud same is reflected in the model.
HTTP_204_NO_CONTENT) entDetails.as_view(), Having gone through the article,
), you would have grasped an idea of
The ‘get_object’ function aims to the absolute basics of generating
obtain the object within the existing In Figure 12, I aimed to change APIs; specifically, how to perform
table having the same primary key just the ‘Last_name’ of the student CRUD operations.
value, i.e., ‘pk’ as the one entered
in the URL. It will return an error By: Mohammed Kamil Khan
message if no entry in our model
The author is currently working at Vamstar as a data discovery expert.
exists with the entered primary key