Django REST Framework API Guide (1) : Requesting Django REST Framework API Guide (2) : Responding to Django REST Framework API Guide (3) : Views Django REST Framework API Guide (4) : Generic Views Django REST Framework API Guide (5) : View Sets Django REST Framework API Guide (6) : Django REST Framework API Guide (7) : Resolve the Django REST Framework API guide
Link to official text
If you are developing reST-based Web API services…… Request.post should be ignored. — Malcom Tredinnick, Django development team
The REST Framework extends the Request class to the standard HttpRequest, with enhancements such as more flexible Request parsing and Request authentication.
Request parsing
REST FramWork’s Request object provides flexible Request parsing, allowing you to process requests using JSON data or other media types in the same way you would normally process form data.
.data
Request. data returns the parsed content of the request subject. This is similar to the standard Request. POST and Request. FILES, with the following features:
- This includes all resolved inputs, file and non-file inputs.
- Support the resolution
POST
Other HTTP methods, for examplePUT
.PATCH
. - More flexible, not only for form data, but also for the same JSON data that can be correctly parsed without any extra processing.
.data
All parse correctly).
.data Specific operation, later talk about ~
.query_params
Request. query_params is the same as request.GET, but the name is easier to understand.
To make your code more legible, use request.query_params instead of request.GET in Django. This should make it obvious to your code that —– any HTTP method type may contain query parameters, not just ‘GET’ requests.
.parsers
The APIView class or @API_VIEW decorator will ensure that this property (.parsers) is automatically set to Parser based on the DEFAULT_PARSER_CLASSES setting in the parser_classes or Settings file set on the view List of instances.
This property is not usually of concern……
If you have to see what’s inside it, you can print it out. It looks something like this:
[<rest_framework.parsers.JSONParser object at 0x7fa850202d68>, <rest_framework.parsers.FormParser object at 0x7fa850202be0>, <rest_framework.parsers.MultiPartParser object at 0x7fa850202860>]
Copy the code
Well, there are three parsers JSONParser, FormParser, MultiPartParser.
Note: Accessing Request. data may raise ParseError if the client sends something in the wrong format. By default, the REST Framework’s APIView class or @API_VIEW decorator will catch errors and return a 400 Bad Request response. If the content of the request sent by the client cannot be parsed (unlike the format error), an UnsupportedMediaType exception will be raised and a 415 UnsupportedMediaType response will be returned by default.
Content negotiation
The request exposes properties that allow you to determine the outcome of the content negotiation phase. This allows you to implement behaviors such as choosing different serialization schemes for different media types.
.accepted_renderer
The renderer instance is selected by the content negotiation phase.
.accepted_media_type
A string that represents the media type accepted during the content negotiation phase.
Authentication
The REST Framework provides flexible authentication methods:
- Different authentication policies can be used in different parts of the API.
- Support the simultaneous use of multiple authentication policies.
- Provides the user and token information associated with the incoming request.
.user
Request. The user will usually return to django. Contrib. The auth. Models. An example of the user, but its behavior depends on the authentication strategy is using.
If the request without the authentication request. The user’s default value is the django contrib. The auth. Models. AnonymousUser instance (anonymous users).
about.user
More on that in the future
.auth
Request. auth returns any additional authentication context. The exact behavior of Request.auth depends on the authentication policy being used, but it may typically be requesting an authenticated token instance.
If the request is unauthenticated or has no context attached, the default value of Request. auth is None.
about.auth
More on that in the future
.authenticators
The APIView class or @API_VIEW decorator will ensure that authentication_classes are set on the view or DEFAULT_AUTHENTICATORS in the Settings based file Set this property (.authenticators) to automatically set as a list of Authentication instances.
** This property is not normally concerned with…… **
Note: It is possible to raise WrappedAttributeError when calling the.user or.auth attribute. These errors stem from authenticator being a standard AttributeError, and in order to prevent them from being modified by external attribute access, it is necessary to promote them back to a different exception type. Python cannot recognize AttributeError from authenticator and immediately assumes that the request object has no.user or.auth attribute. The Authenticator needs to be fixed.
Say a few words more
. Authenticators actually stores a list of authenticators that are currently in use, which looks something like this when printed:
[<rest_framework.authentication.SessionAuthentication object at 0x7f8ae4528710>, <rest_framework.authentication.BasicAuthentication object at 0x7f8ae45286d8>]
Copy the code
You can see that the authenticators used here include SessionAuthentication and BasicAuthentication.
Browser enhancement
The REST Framework supports browser-based PUT, PATCH, and DELETE forms.
.method
Request. method returns an uppercase string representation of the request HTTP method. Such as GET, POST… .
Transparently supports browser-based PUT, PATCH, and DELETE forms.
More on that later
.content_type
Request. content_type Returns a string object representing the media type of the body of the HTTP request (e.g. Text /plain, text/ HTML, etc.), or an empty string if no media type is provided.
Direct access to this property is usually not required and generally relies on the default request resolution behavior with the REST framework.
It is not recommended to use request.meta. get(‘HTTP_CONTENT_TYPE’) to get content Type.
More on that later
.stream
Request. stream returns a stream representing the content of the request body.
Direct access to this property is usually not required and generally relies on the default request resolution behavior with the REST framework.
Standard HttpRequest attribute
Since the REST framework’s Request extends to Django’s HttpRequest, all other standard properties and methods are also available. For example, request.META and Request. session dictionaries can be used normally.
Please note that Request does not inherit from HttpRequest for implementation reasons. Instead, it uses compositional extensions.
The last
Written by Google Translate and personal understanding, please spray gently (good side).
Then, I plan to go through the API guide on the official website, so I plan to organize it into Chinese, convenient for later reading, and write it by myself to make it easier to understand and memorize.
Django Rest Framework Chinese version tutorial