I am building a Django application that exposes a REST API by which users can query my application's models. I'm following the instructions here
My Route looks like this in mySites url.py:
router.register(r'myObjects', views.MyObjectsViewSet)
....
url(r'^api/', include(router.urls)),
My Serializer looks like this:
class MyObjectSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = MyObject
fields = ('id', 'name',)
My Viewset looks like this:
class MyObjectsViewSet(viewsets.ModelViewSet):
queryset = MyObjects.objects.all()
serializer_class = MyObjectSerializer
When I hit the API /api/myObjects/ it gives me a listing of all the myObject models. When I hit the API /api/myObjects/60/ it gives me only the myObject with id == 60. Great so far!
However, I want to change the logic of MyObjectsViewSet() such that I can manipulate/change what it returns when I hit /api/myObjects/60/. So instead of doing MyObjects.objects.all() I want to do something more complex based on the myObject ID of 60. But how can I do that?? In this viewset, how can I grab that number 60? It is not passed in as an argument. But I really need it!
A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as . get() or . post() , and instead provides actions such as . list() and . create() .
APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.
While regular views act as handlers for HTTP methods, viewsets give you actions, like create or list . The great thing about viewsets is how they make your code consistent and save you from repetition. Every time you write views that should do more than one thing, a viewset is the thing that you want to go for.
As part of our work to make sharp web apps at Caktus, we frequently create API endpoints that allow other software to interact with a server. Oftentimes this means using a frontend app (React, Vue, or Angular), though it could also mean connecting some other piece of software to interact with a server.
In your router, register one more url with:
router.register(r'myObjects/(?P<id>\d+)', views.MyObjectsViewSet)
and in your viewset you can grab the id with:
self.kwargs['id']
Ref: http://www.django-rest-framework.org/api-guide/filtering#filtering-against-the-url
I think you can update your view for multiple operations such as
class RetrieveUpdateAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
generics.SingleObjectAPIView):
"""
Concrete view for retrieving or updating a model instance.
FIXME: the newest version of rest_framework has this class
"""
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def create(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
Please watch this tutorial it will help you to understand the REST framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With