2

I am pretty new to Django and REST and I want to be able to specify a value and have the REST api only return a row where that value is met. Kinda like in sql select * from exampleTBL where id = 1 and then the first row is returned. But it would be done through the url: www.website/api/tmpHost/?id=1 and t hen the first row is returned through the REST API

My view looks like:

class tmp_HostList(APIView):
    def get (self, request, format=None):
        tmp_hosts = tmp_Host.objects.all()
        serializer = tmp_HostSerializer(tmp_hosts, many=True, context={'request': request})
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = tmp_HostSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

my url looks like:

url(r'^api/tmpHost/$', views.tmp_HostList.as_view()),

my serializer looks like:

class tmp_HostSerializer(DynamicFieldsMixin,  serializers.ModelSerializer):
    class Meta:
        model = tmp_Host
        fields = '__all__'

How would I go about doing this? I've seen solutions around here but they don't seem to work for me. The differences where that I use APIView and my serializer line would be: serializer = tmp_HostSerializer(tmp_hosts, many=True, context={'request': request}) while theirs would be simple like: serializer = tmp_HostSerializer

1 Answer 1

1

The simplest way is just check for get parameters and return a filtered object by the parameter:

from django.shortcuts import get_object_or_404

class tmp_HostList(APIView):

    def get (self, request, format=None):
        param = request.GET.get('id')
        if param:
            tmp_host = get_object_or_404(Host, id=param)
            serializer = tmp_HostSerializer(tmp_host)
        else:
            tmp_hosts = tmp_Host.objects.all()
            serializer = tmp_HostSerializer(tmp_hosts, many=True)
        return Response(serializer.data)

There is also built in filtering for generic views and viewsets doc link
But the best choice is create a separate view for detail page or use viewset/generic views.
So your view is stay the same and you add a new one for detail page.
urls:
url(r'^api/tmpHost/(?P<id>\d+)$', views.tmp_HostList.as_view())
views:

class tmp_HostDetail(APIView):

    def get (self, request, id=None, format=None):
        tmp_host = get_object_or_404(Host, id=id)
        serializer = tmp_HostSerializer(tmp_host)
        return Response(serializer.data)
Sign up to request clarification or add additional context in comments.

2 Comments

Where did you get the variable 'q' or is that supposed to be replaced by a filter?
@JustinBraham it should be param. I make a mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.