request/response #
Django HTTPRequest
=> DRF Request
Django HTTPResponse
=> DRF Response
view #
class-based views #
Django View
=> DRF APIView
function-based #
generic views #
viewsets #
APIView
=> ViewSet
GenericAPIView
=> GenericViewSet
GenericAPIView
=> ModelViewSet
serializers #
需要解决的问题包括整体
的, field
的序列化和反序列化,relation
的处理
- 序列化时候的输出数据预处理用
.to_representation()
- 反序列化的输入数据需要预处理用
.to_internal_value()
例如 `serializers.DatetimeField`` 不能允许空字符输入,可以如下:
def to_internal_value(self, data):
data['date'] = None if data['date'] == '' else data['date']
return data
@action url_path
#
https://ilovedjango.com/django/rest-api-framework/views/tips/sub/extra-actions-for-routing/