I have a url that looks like this:
url(r'^client_profile/address/(?P<id>.+)/$', views.ClientProfileAddressView.as_view())
And an APIView:
class ClientProfileAddressView(APIView):
renderer_classes = (JSONRenderer,)
permission_classes = (IsAuthenticated,)
def put(self, request):
....
def get(self, request):
....
In both put
and get
, I need to access the id
url kwarg, the first one to update the object, the second to update it. How can I access the url argument in those methods?
Function-based Views @api_view is a decorator that converts a function-based view into an APIView subclass (thus providing the Response and Request classes). It takes a list of allowed methods for the view as an argument. Curious how DRF converts function-based views into APIView subclasses?
APIView is a base class. It doesn't assume much and will allow you to plug pretty much anything to it. GenericAPIView is meant to work with Django's Models. It doesn't assume much beyond all the bells and whistles the Model introspection can provide.
APIView class is a subclass of a Django View class. APIView classes are different from regular View classes in the following ways: Requests passed will be REST framework's Request instances, not Django's HttpRequest instances. Handler methods may return REST framework, instead of Django's HttpResponse.
The root QuerySet provided by the Manager describes all objects in the database table. Usually, though, you'll need to select only a subset of the complete set of objects. The default behavior of REST framework's generic list views is to return the entire queryset for a model manager.
This should work:
def put(self, request, *args, **kwargs):
id = kwargs.get('id', 'Default Value if not there')
def get(self, request, *args, **kwargs):
id = kwargs.get('id', 'Default Value if not there')
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