Add the following line in the urls.py file (server_config folder)
from django import urls
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="API_NAME",
default_version='v3',
description="Description",
terms_of_service="URL",
contact=openapi.Contact(email="EMAIL_ID"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
path('admin/', admin.site.urls),
path('api/',include('api.urls')),
path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('doc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
Add 2 Files in the api folder (api folder)
urls.py
serializers.py
Add the following line in the models.py file (api folder)
from django.db import models
class Basetable(models.Model):
id = models.AutoField(primary_key=True)
productname = models.CharField(max_length=100)
description = models.CharField(max_length=100)
Add the following line in the serializers.py file (api folder)
from django.db.models import fields
from rest_framework import serializers
from .models import *
class BasetableSerializers(serializers.ModelSerializer):
class Meta:
model = Basetable
fields = '__all__'
Add the following line in the views.py file (api folder)
from django.shortcuts import render
from .models import *
from .serializers import *
from rest_framework import mixins
from rest_framework import generics
class PostAPI(mixins.ListModelMixin,mixins.CreateModelMixin,generics.GenericAPIView):
queryset = Basetable.objects.all()
serializer_class = BasetableSerializers
lookup_field = 'id_number'
def get(sef, request, id_number = None):
if id_number:
return sef.retrieve(request)
else:
return sef.list(request)
Add the following line in the urls.py file (api folder)
from django.urls import path
from .views import *
urlpatterns = [
path('post/',PostAPI.as_view()),
]
if you change the default database to a MySQL database then add the following lines in the settings.py file