Django rest framework API Guide 요약본
[ Requests ]
■ .data
- request.data returns the parsed content of the body:
- includes all parsed content, including file and non-file inputs
- supports parsing the content of HTTP methods other than POST, such as PUT and PATCH requests
- supports request parsing other than form data
■ .query_params
- is a more correctly named synonym for request.GET
■ .method
- Returns the uppercased string representation of the request’s HTTP method
[ Responses ]
■ Response(data, status=None, template_name=None, headers=None, content_type=None)
- .data : the unrendered, serialized data of the response
- .status_code : the numeric status code of the HTTP response
- .content: the rendered content of the response
- .template_name: only required if HTMLRenderer or some other custom template renderer is the accepted renderer for the response
■ .render()
- .render(data, accepted_media_type, renderer_context)
- Called to render the serialized data of the response into the final response content
[ Views ]
■ APIView
- Requests passed to the handler methods will be REST framework’s Request instances, not Django’s HttpRequest [ instances ]
- Handler methods may return REST framework’s Response, instead of Django’s HttpResponse
■ @api_view()
- takes a list of HTTP methods that the view responds to
[ Parsers ]
■ JSONParser
- Parses JSON request content
- .media_type: application/json
■ FormParser
- Parses HTML form content
- .media_type: application/x-www-form-urlencoded
■ MultiPartParser
- Parses multipart HTML form content, which supports file uploads
- .media_type: multipart/form-data
■ FileUploadParser
- Parses raw file upload content
- .media_type: */*
[ Renderers ]
■ JSONRenderer
- Renders the request data into JSON, using utf-8 encoding
■ TemplateHTMLRenderer
- Renders data to HTML, data passed to response does not need to be serialized
[ Serializers ]
■ allow complex data such as querysets and model instances to be converted to native python datatypes that can then be easily rendered into JSON, XML or other content types
■ Deserialization
- allows parsed data to be converted back into complex types, after first validating the incoming data
■ ModelSerializer
- provides a shortcut automatically creates a Serializer class with fields that correspond to the Model fields
[ format_suffix_patterns ]
■ format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None)
- returns a URL pattern list which includes format suffix patterns appended to each of the URL patterns provided
[ Returning URLs ]
■ it’s a better practice to return absolute URIs from Web APIs, rather than returning relative URIs:
- it’s more explicit
- leaves less work for API clients
- makes it easy to do things like markup HTML representations with hyperlinks
■ reverse
- reverse(viewname, *args, **kwargs)
- returns a fully qualified URL using the request to determine the host and port
참조 [ Reference ] : https://www.django-rest-framework.org/api-guide/
'Server-Side Script > Python' 카테고리의 다른 글
장고[django] cookiecutter 이용하기 (0) | 2021.11.08 |
---|---|
python 특별한 methods (0) | 2021.10.27 |
python socket통신 (0) | 2021.10.20 |
django db + migration (0) | 2021.09.30 |
django project 설정 / setup (0) | 2021.09.27 |
댓글