You may use these instructions to develop a GET API in
Django. I'll provide you a thorough explanation-filled guide to assist you
grasp every section. Keep in mind that this is only a simple example, and you
may need to modify it to suit your own needs.
Install Django:
YA high-level Python web framework called Django promotes
efficient development and simple, straightforward design. You may use the
following command to install it:
Pip install Django
Use this command to install Django's most recent version.
Make a Django Project:
A project in Django is a group of configurations and
parameters for a particular website. Use the following command to start a new
project:
django-admin startproject yourprojectname
Put the name you choose for your project in lieu of
yourprojectname.
How to Make a Django App:
Applications, which are modules that manage certain
capabilities, are the division units of Django projects. Make a new application
with:
cd yourprojectname
python manage.py startapp yourappname
Put the name you wish to give your app in lieu of
yourappname.
Describe a Model:
Define a basic model in the models.py file located inside
your application. As an illustration, let's make a model named Item:
from django.db import models
Class Item(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
An object with a name and description is represented by this
model.
Run Migrations:
Django applies schema modifications to the database using
migrations. To implement the model modifications in the database, execute the
subsequent commands:
python manage.py makemigrations
python mange.py migrate
Make a Serializer:
Django serializers are used to translate complicated data
types—such as instances of Django models—into Python data types that are
readily converted into JSON. Make your item model's serializer:
from rest_framework impoert serializers
from .models import Item
Class ItemSerializer(serializers.ModelSerializer ):
Class Meta:
model = Item
fields = [“name”, “descripstion”]
The item model will be serialized into JSON using this
serializer.
Make Views:
Django views are in charge of handling requests and
providing pertinent answers. Make a views file in which the logic for your API
endpoint is defined:
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Item
from .serializers import ItemSerializer
@api_view(['GET'])
def get_items(request):
items = Item.objects.all(
serializer = ItemSerializer(items, many=True)
return Response(serializer.data)
All items are serialized by this view function using the
ItemSerializer and returned in the API response.
Set URLs:
Make sure the new endpoint is included in the URLs for your
application.
from django.urls import path
from .views import get_items
urlpatterns = [
path('api/items/', get_items),
]
Launch the development server:
python manage.py runserver
The URL http://localhost:8000/yourappname/api/items/ is now
where you may access your API. The serialized objects from your database will
be returned in a JSON response as a result.
Don't forget to change yourprojectname and yourappname and
modify the model fields to suit your needs. This is a rudimentary example; in a
real-world situation, you may wish to add additional features like pagination,
error handling, authentication, etc. Additionally, if you're developing APIs
with more complex functionality, think about utilizing the Django Rest
Framework.
1 Comments
Jxhxjd
ReplyDelete