Django REST framework is a powerful and flexible toolkit for building Web API's.
-
Overview of the created API
@api_view(['GET']) def apiOverview(request): api_urls = { 'Pizzalist': '/pizza-list/', 'Detail View': '/pizza-detail/<int:pk>/', 'Create': '/pizza-create/', 'Update': '/pizza-update/<int:pk>/', 'Delete': '/pizza-delete/<int:pk>/', } return Response(api_urls)
-
Creating a pizza order api
@api_view(['POST']) def createpizza(request): serializer = PizzaSerializers(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data)
-
Getting a single pizza order detail api
@api_view(['GET']) def pizzadetail(request, id): pizza = Pizza.objects.get(id=id) serailzer = PizzaSerializers(pizza, many=False) return Response(serailzer.data)
-
Updating a pizza order api
@api_view(['POST']) def updatepizza(request, id): pizza = Pizza.objects.get(id=id) serializer = PizzaSerializers(instance=pizza, data=request.data) if serializer.is_valid(): serializer.save() return Response("Updated successfully")
-
Deleting a pizza order api
@api_view(['DELETE']) def deletepizza(request, id): pizza = Pizza.objects.get(id=id) pizza.delete() return Response("Deleted successfully")
-
Displaying all the pizza orders api
@api_view(['GET']) def pizzalist(request): pizzas = Pizza.objects.all() serializer = PizzaSerializers(pizzas, many=True) return Response(serializer.data)
-
serializers.py
from rest_framework import serializers from .models import Pizza class PizzaSerializers(serializers.ModelSerializer): class Meta: model = Pizza fields = "__all__"
-
models.py
from django.db import models class Pizza(models.Model): type = models.CharField(max_length=20, blank=False, verbose_name="Type of Pizza", default=" ") sizes = models.CharField(max_length=50, blank=False, verbose_name="Size of Pizza", default=" ") toppings = models.CharField(max_length=200, blank=False, verbose_name="Toppings", default=" ")
-
clone or download the project from here
-
after downloading the zip file, unzip the project and open in your code editor.
-
Now, Create a virtual environment.
python -m venv venv
-
Now run the following commands to run this project
.\venv\scritps\activate python manage.py makemigrations python manage.py migrate python manage.py runserver
-
Note:
This Project will open in http://127.0.0.1:8000/ and not in http://localhost:8000/ Because of CORS is disabled. MongoDB should be installed in your system.
- Completed Taks
- A Pizza can be of multiple types: Regular or Square
- A Pizza can be of multiple sizes: Small, Medium, Large, etc.
- A Pizza can consist of many toppings out of the following (Onion, Tomato, Corn, Capsicum, Cheese, Jalapeno etc.), the choice of toppings should not be limited to the ones mentioned above, the user should be allowed to add any type of topping at any point of time)
- API
- Create an API endpoint to create regular pizza and a square pizza.
- Create an API endpoint which lists the information about all the stored pizza, the response of this should also contain the information about the toppings, size and type of Pizza.
- Allow filtering the list of pizza returned by the API based on Size & Type of Pizza.
- Create an API endpoint that allows the user to edit or delete any pizza from the database.