simple API :
View function:
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view()
def product_list(request):
return Response("data")
Creating Serializer:
render(dict)
which converts the dictionary into json objectsfrom rest_framework import serializer
class ProductSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
unit_price = serializers.DecimalField()
Serializing Objects:
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Product
from .serializer import ProductSerializers
@api_view()
def product_list(request):
return Response("data")
@api_view()
def product_detail(request, id):
product = Product.objects.get(pk=id)
# this converts the product model into dict
serializer = ProductSerializer(product)
# now jsonrender converts the dict to json object
return Response(serializer.data)
Converting string to int fields:
This will convert data from default string to the actual data type of the attributes
REST_FRAMEWORK = {
'COERCE_DECIMAL_TO_STRING':False
}