简体   繁体   中英

Consuming Django Rest Api in the same project

I'm pretty new to django's rest framework and I built my first example api using the official tutorial here . But I have no idea how to consume this api's data into another app in the same project such it's data can be rendered into HTML.

Suppose I create an API students for students(with their details) in a school. Now how do I use this api in the same project to display number of student in schools and their details.

Most of the tutorials or explanations online are for third party API's and I can't figure out how to proceed. Thanks in advance.

models.py

class Test(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    test_name = models.CharField(max_length=200,default='simple blood test',blank=False)
    subject = models.CharField(max_length=100,default='')

def __str__(self):
    return self.test_name

class Person(models.Model):
    tests = models.ManyToManyField(Test)
    title = models.CharField(max_length=3,default="mr",blank=False)
    name = models.CharField(max_length=50,default='',blank=False)

def __str__(self):
    return self.name

views.py

class PersonList(generics.ListCreateAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)


class PersonDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer

serializers.py

class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = Test
        fields = ('test_name','subject')

class PersonSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')
    tests = TestSerializer(many=True, read_only=True)
    class Meta:
        model = Person
        fields = ('url','id','name') 

This is my API definiton. I want to create another app to display data such list of all students and details about them etc.

You will have to hit your endpoints in your consuming view, the easiest way to do this is with the requests library. First install the library:

pip install requests

Then use it in your consuming view:

def consumer_view(request):
    response = requests.get('http://your-url.com/your-endpoint')
    # do what you need to do here

You can use response.json() to get the JSON response form your API as a Python dictionary. If you are just using ./manage.py runserver your URL will be:

http:localhost:8000/your-endpoint

or

http://192.168.0.1:8000/your-endpoint

This way of consuming an API is somewhat redundant if you are working completely within Django. It's often much easier to use the ORM in these cases. However if you are making the API to be available for outside use (either publicly or via API keys) then this approach makes sense.

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