django-rest-framework Bulk operations
来源:互联网 发布:mac如何访问共享文件夹 编辑:程序博客网 时间:2024/06/16 20:26
Bulk operations
New in DRF-extensions 0.2.4
Bulk operations allows you to perform operations over set of objects with one request. There is third-party package
django-rest-framework-bulk with support for all CRUD methods, but it iterates over every
instance in bulk operation, serializes it and only after that executes operation.
It plays nice with create
or update
operations, but becomes unacceptable with partial update
and delete
methods over the QuerySet
. Such kind of QuerySet
could contain thousands of objects and should be performed as database query over the set at once.
Please note - DRF-extensions bulk operations applies over QuerySet
, not over instances. It means that:
- No serializer’s
save
ordelete
methods would be called - No viewset’s
pre_save
,post_save
,pre_delete
andpost_delete
would be called - No model signals would be called
Safety
Bulk operations are very dangerous in case of making stupid mistakes. For example you wanted to delete user instance
with DELETE
request from your client application.
# RequestDELETE /users/1/ HTTP/1.1Accept: application/json# ResponseHTTP/1.1 204 NO CONTENTContent-Type: application/json; charset=UTF-8
That was example of successful deletion. But there is the common situation when client could not get instance id and sends
request to endpoint without it:
# RequestDELETE /users/ HTTP/1.1Accept: application/json# ResponseHTTP/1.1 204 NO CONTENTContent-Type: application/json; charset=UTF-8
If you used bulk destroy mixin for /users/
endpoint, then all your user objects would be deleted.
To protect from such confusions DRF-extensions asks you to send X-BULK-OPERATION
header
for every bulk operation request. With this protection previous example would not delete any user instances:
# RequestDELETE /users/ HTTP/1.1Accept: application/json# ResponseHTTP/1.1 400 BAD REQUESTContent-Type: application/json; charset=UTF-8{ "detail": "Header 'X-BULK-OPERATION' should be provided for bulk operation."}
With X-BULK-OPERATION
header it works as expected - deletes all user instances:
# RequestDELETE /users/ HTTP/1.1Accept: application/jsonX-BULK-OPERATION: true# ResponseHTTP/1.1 204 NO CONTENTContent-Type: application/json; charset=UTF-8
You can change bulk operation header name in settings:
REST_FRAMEWORK_EXTENSIONS = { 'DEFAULT_BULK_OPERATION_HEADER_NAME': 'X-CUSTOM-BULK-OPERATION'}
To turn off protection you can set DEFAULT_BULK_OPERATION_HEADER_NAME
as None
.
Bulk destroy
This mixin allows you to delete many instances with one DELETE
request.
from rest_framework_extensions.bulk_operations.mixins import ListDestroyModelMixinclass UserViewSet(ListDestroyModelMixin, viewsets.ModelViewSet): serializer_class = UserSerializer
Bulk destroy example - delete all users which emails ends with gmail.com
:
# RequestDELETE /users/?email__endswith=gmail.com HTTP/1.1Accept: application/jsonX-BULK-OPERATION: true# ResponseHTTP/1.1 204 NO CONTENTContent-Type: application/json; charset=UTF-8
Bulk update
This mixin allows you to update many instances with one PATCH
request. Note, that this mixin works only with partial update.
from rest_framework_extensions.mixins import ListUpdateModelMixinclass UserViewSet(ListUpdateModelMixin, viewsets.ModelViewSet): serializer_class = UserSerializer
Bulk partial update example - set email_provider
of every user as google
, if it’s email ends with gmail.com
:
# RequestPATCH /users/?email__endswith=gmail.com HTTP/1.1Accept: application/jsonX-BULK-OPERATION: true{"email_provider": "google"}# ResponseHTTP/1.1 204 NO CONTENTContent-Type: application/json; charset=UTF-8
Settings
DRF-extensions follows Django Rest Framework approach in settings implementation.
In Django Rest Framework you specify custom settings by changing REST_FRAMEWORK
variable in settings file:
REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.YAMLRenderer', ), 'DEFAULT_PARSER_CLASSES': ( 'rest_framework.parsers.YAMLParser', )}
In DRF-extensions there is a magic variable too called REST_FRAMEWORK_EXTENSIONS
:
REST_FRAMEWORK_EXTENSIONS = { 'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60 * 15}
- django-rest-framework Bulk operations
- Django REST framework
- Django REST framework API
- Django REST framework
- Django-Rest-Framework 教程
- django rest framework Serializer
- Django Rest Framework
- django rest framework quickstart
- Django rest framework
- Django rest framework --- Routers
- Django rest framework ---Serializers
- Django-Rest-Framework 入门
- Django Rest Framework - 初识
- django-REST framework
- Django rest framework权限
- Django REST framework-Quickstart
- Django Rest framework-学习
- django-rest-framework安装
- PAT-B 1011.A+B和C (15)
- 算法训练 Beaver's Calculator/codeforces 207A1 排序+贪心
- 移动APP测试中的功能与非功能测试
- SQL SERVER——CPU问题定位与解决
- jquery之ajaxfileupload异步上传插件
- django-rest-framework Bulk operations
- Linux GCC常用命令
- Android App 评价/跳转到应用市场
- 很全很强大国内值得关注的官方API集合(太幸福啦)
- 《JS高程(3)》DOM扩展-API选择符-第11章笔记(18)
- C++学习一虚函数的实现方式
- 文章标题
- easyui datagrid load 参数表单序列化
- Key Concepts (Mastering CMake)