Skip to content

Ndhlovu1/Menu-Api-Crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Menu Management Api Application

A Django rest framework application used to showcase an api performing full crud operations

Please Visit my repository on https://github.com/Ndhlovu1/django-crud-api-system

To properly implement the Api's

> 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()
Create the serializers.py file to implement the serializers as connecters into the database
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']
Create the necessary AppFolders/views.py file to enable the drf api's to be viewed
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()),
    
]

Screenshot from 2022-12-28 11-30-04

The App level urls.py file

Screenshot from 2022-12-28 11-30-13

Add the 'rest_framework' into the installed apps

Screenshot from 2022-12-28 11-32-43

A Get Call

Screenshot from 2022-12-28 11-08-39

An Inset/Put Call

Screenshot from 2022-12-28 11-09-18

About

A django DRF CRUD Application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages