简体   繁体   中英

how to pass data from Django to react without database

I am doing a dashboard app using Django and react. The user 's data is from Dynamics CRM API. I created a function in python to fetch all the info I need for the dashboard. What I want to do is to first fetch the data from Dynamics Api of a user at backend and then pass it to React component. Since the data from Dynamics Api is updating,I don't want to save the fetched data to my Django database. I know the REST api needs a database to function, but is there a way to pass data to React without using api?

You can use the Serializer class directly.

from rest_framework import serializers

class YourSerializer(serializers.Serializer):
   field1 = serializers.IntegerField()
   field2 = serializers.IntegerField()

and for the view

from rest_framework import views
from rest_framework.response import Response

from .serializers import YourSerializer

class YourView(views.APIView):

    def get(self, request):
        yourdata= [{"field1": 1, "field2": 2}, {"field1": 1, "field2": 3}]
        results = YourSerializer(yourdata, many=True).data
        return Response(results)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM