Please Visit my repository on https://github.com/Ndhlovu1/django-crud-api-system
> pipenv install djangorestframework
You should also see the djangorestframework file added into your Pipfile that keeps your dependencies
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
django = "*"
djangorestframework = "*"
[dev-packages]
[requires]
python_version = "3.8"
# Go into the settings.py file and change the line below
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
] # The default INSTALLED_APPS LOOKS LIKE THIS LIST
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'menuApiApp',
]
# Dont remove that last coma,
# The names of your apps must also be added into this list element
Go into the AppFolders/models.py file and add the table attributes that'll be present as forms in the Api calls
class MenuItem(models.Model):
title = models.CharField(max_length=255)
price = models.DecimalField(max_digits=6, decimal_places=2)
inventory = models.SmallIntegerField()
from rest_framework import serializers
from .models import MenuItem
class menuItSerializer(serializers.Serializer):
id = serializers.IntegerField()
title = serializers.CharField(max_length=255)
class MenuItemSerializer(serializers.ModelSerializer):
#The Meta class is only needed if the serializers.ModelSerializer is imported
class Meta:
model = MenuItem
fields = ['id','title','price','inventory']
from rest_framework import generics
from .models import MenuItem
from .serializers import MenuItemSerializer
class MenuItemsView(generics.ListCreateAPIView):
#Your django language database query
queryset = MenuItem.objects.all() #Similar to Select * FROM MenuItem;
serializer_class = MenuItemSerializer #Enabling that db connection
#Viewing an Item One at a time
class SingleMenuItemView(generics.RetrieveUpdateAPIView, generics.DestroyAPIView):
queryset = MenuItem.objects.all()
serializer_class = MenuItemSerializer
The Project/urls.py file is going to inherit the App/urls.py hence add the code below into a newly created urls.py file in the app folder
from django.urls import path
from . import views
urlpatterns = [
path('menu-items', views.MenuItemsView.as_view()),
path('menu-items/<int:pk>', views.SingleMenuItemView.as_view()),
]