From e98235ca276527de184e7a815a6e26f76efbd0a3 Mon Sep 17 00:00:00 2001 From: xinwen Date: Wed, 29 Jul 2020 17:04:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(serializer):=20=E8=AE=BE=E7=BD=AE=20`B?= =?UTF-8?q?ulkSerializerMixin`=20=E7=9A=84=E9=BB=98=E8=AE=A4=20`ListSerial?= =?UTF-8?q?izer`=20=E4=B8=BA=20`AdaptedBulkListSerializer`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/mixins/serializers.py | 10 +++++++++- apps/terminal/serializers/terminal.py | 1 - 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/common/mixins/serializers.py b/apps/common/mixins/serializers.py index 5c3a243cf..d9df17e1d 100644 --- a/apps/common/mixins/serializers.py +++ b/apps/common/mixins/serializers.py @@ -8,7 +8,6 @@ from rest_framework.utils import html from rest_framework.settings import api_settings from rest_framework.exceptions import ValidationError from rest_framework.fields import SkipField, empty - __all__ = ['BulkSerializerMixin', 'BulkListSerializerMixin', 'CommonSerializerMixin', 'CommonBulkSerializerMixin'] @@ -50,6 +49,15 @@ class BulkSerializerMixin(object): self.initial_data = data return super().run_validation(data) + @classmethod + def many_init(cls, *args, **kwargs): + meta = getattr(cls, 'Meta', None) + assert meta is not None, 'Must have `Meta`' + if not hasattr(meta, 'list_serializer_class'): + from common.drf.serializers import AdaptedBulkListSerializer + meta.list_serializer_class = AdaptedBulkListSerializer + return super(BulkSerializerMixin, cls).many_init(*args, **kwargs) + class BulkListSerializerMixin(object): """ diff --git a/apps/terminal/serializers/terminal.py b/apps/terminal/serializers/terminal.py index bfe992b19..b643dd16a 100644 --- a/apps/terminal/serializers/terminal.py +++ b/apps/terminal/serializers/terminal.py @@ -12,7 +12,6 @@ class TerminalSerializer(BulkModelSerializer): class Meta: model = Terminal - list_serializer_class = AdaptedBulkListSerializer fields = [ 'id', 'name', 'remote_addr', 'http_port', 'ssh_port', 'comment', 'is_accepted', "is_active", 'session_online',