Home > OS >  API Query in Django Rest Framework
API Query in Django Rest Framework

Time:11-05

I have created an API from Database, I can view the API but I am unable to do a query via URL for example: 127.0.0.1:8000/author?author_id=9, I am not sure where to add the query code. I want to filter using fields. Here is my models.py

class AuthorAPI(models.Model):
    author_id=models.IntegerField()
    name=models.TextField()
    author_img_url=models.TextField()
    title=models.TextField()
    first_published_at=models.DateTimeField()
    excerpt=models.TextField()

    class Meta:
        db_table = 'view_author'

serializers.py

from rest_framework import serializers
from .models import SortAPI, AuthorAPI
class AuthorAPISerializer(serializers.ModelSerializer):
    class Meta:
        model=AuthorAPI
        fields='__all__'

views.py

from .serializers import APISerializer,AuthorAPISerializer
from .models import SortAPI, AuthorAPI
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET'])
def getauthor(request):
    if request.method == 'GET':
        results = AuthorAPI.objects.all()
        serialize = AuthorAPISerializer(results, many=True)
        return Response(serialize.data)

CodePudding user response:

In your views, use a ModelViewset

And add the fliter_backend attribute:

filter_backends = [django_filters.rest_framework.DjangoFilterBackend]

See here in the docs:

https://www.django-rest-framework.org/api-guide/filtering/#setting-filter-backends

class AuthorViewset(viewsets.ReadOnlyModelViewset):
    serializer_class = AuthorAPISerializer
    queryset = AuthorAPI.objects.all()
    filter_backends = [django_filters.rest_framework.DjangoFilterBackend]

IMPORTANT

Using django_filter will require you to install additional requirements, buts its well worth it, see installation steps for django_filter here:

https://django-filter.readthedocs.io/en/stable/guide/install.html

And in your urls.py you need to register your viewser with a SimpleRouter as described in the docs here:

https://www.django-rest-framework.org/api-guide/viewsets/#example

Once complete, if you browse to /author you should see some filter controls available, et voila

CodePudding user response:

You can use request.GET to get data from URL parameters.

Give this a try

@api_view(['GET'])
def getauthor(request):
    if request.method == 'GET':

        results = AuthorAPI.objects.all()

        # get author_id from the url query parameter
        author_id = request.GET.get('author_id', None)
        
        #if author_id is present in the url query parameter then filter the resluts queryset based on the author_id
        if author_id:
            results = results.filter(author_id=author_id)

        serialize = AuthorAPISerializer(results, many=True)
        return Response(serialize.data)
  • Related