[Django]-Django: Model Queryset to Pandas to Django Rest Framework

4๐Ÿ‘

โœ…

So i figured it out. First, you need to install django-pandas and django rest framework (but no need to install django REST pandas)

models.py and serializers.py files stay the same as above, but the views file is different

from rest_framework.views import APIView
from rest_framework.response import Response

from django_pandas.io import read_frame   # Import django_pandas.io read frame

from pages.models import Fund
from .serializers import FundSerializer

class TestData(APIView):
    authentication_classes = []
    permission_classes = []

    def get(self, request, format=None):
        data = Fund.objects.all()        # Perform database query
        df = read_frame(data)            # Transform queryset into pandas dataframe 
        df['testcolumn'] = "testdata"    # Perform some Pandas Operation with dataframe
        return Response(df)              # Return the result in JSON via Django REST Framework
๐Ÿ‘คDaniel

Leave a comment