Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 its 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 the number of students 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.

like image 350
WutWut Avatar asked Oct 31 '25 15:10

WutWut


1 Answers

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.

like image 116
kylieCatt Avatar answered Nov 03 '25 07:11

kylieCatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!