Browse Source

refactor(serializer): 设置 `BulkSerializerMixin` 的默认 `ListSerializer` 为 `AdaptedBulkListSerializer`

pull/4429/head
xinwen 4 years ago committed by 老广
parent
commit
e98235ca27
  1. 10
      apps/common/mixins/serializers.py
  2. 1
      apps/terminal/serializers/terminal.py

10
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):
"""

1
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',

Loading…
Cancel
Save