I am using DRF for the first time and I would like to know what the "format" in the following code snippet from their website do:
class CommentList(APIView):
def get(self, request, format=None):
# do stuff...
def post(self, request, format=None):
# do stuff...
I read the docs, but I am not sure how it works still. Can someone enlighten me with an example? Thanks
For example,
Suppose their is an APIView with following code :
class HelloAPIView(APIView):
def get(self, request, format):
# do stuff...
def post(self, request, format):
# do stuff...
And endpoint url for HelloAPIView is :
http://example.com/api/users
Now if u want the data in response to be in json format then the URL you would access will be like
http://example.com/api/users.json
So when you access urls like above , then value " json " will be passed as 3rd argument (1st is self , 2nd is request and 3rd is format) to get() or post() methods of HelloAPIView.
So basically format parameter is used to define in which format you want the response.
For more details refer
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