本文整理汇总了Python中rest_framework.serializers.ListSerializer方法的典型用法代码示例。如果您正苦于以下问题:Python serializers.ListSerializer方法的具体用法?Python serializers.ListSerializer怎么用?Python serializers.ListSerializer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.serializers
的用法示例。
在下文中一共展示了serializers.ListSerializer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child))
self.permission_classes = self.child.permission_classes
self.check_permission = self.child.check_permission
self.allow_empty = kwargs.pop('allow_empty', True)
assert self.child is not None, '`child` is a required argument.'
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
# TODO: diagnose/address bad-super-call issue
# pylint: disable=bad-super-call
super(ListSerializer, self).__init__(*args, **kwargs)
self.child.bind(field_name='', parent=self)
示例2: __init__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def __init__(self, serializer, data):
"""Initializes and runs the processor.
Arguments:
serializer: a DREST serializer
data: the serializer's representation
"""
if isinstance(serializer, ListSerializer):
serializer = serializer.child
self.data = {}
self.seen = defaultdict(set)
self.plural_name = serializer.get_plural_name()
self.name = serializer.get_name()
# process the data, optionally sideloading
self.process(data)
# add the primary resource data into the response data
resource_name = self.name if isinstance(
data,
dict
) else self.plural_name
self.data[resource_name] = data
示例3: to_internal_value
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def to_internal_value(self, data):
"""
This implements the same relevant logic as ListSerializer except that if one or more items fail validation,
processing for other items that did not fail will continue.
"""
if not isinstance(data, list):
message = self.error_messages['not_a_list'].format(
input_type=type(data).__name__
)
raise serializers.ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
})
ret = []
for item in data:
try:
validated = self.child.run_validation(item)
except serializers.ValidationError as exc:
ret.append(exc.detail)
else:
ret.append(validated)
return ret
示例4: __init__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def __init__(self, serializer):
"""
:param serializer: DjangoRestFramework Serializer
"""
if isinstance(serializer, ListSerializer):
serializer = serializer.child
self.serializer = serializer
self.name = self._get_name()
self.fields = self._collect_fields()
示例5: __init__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def __init__(self, field, parent, only_fields, include_fields):
self.field = field
self.parent = parent
self.is_many = isinstance(field, serializers.ListSerializer) and isinstance(field.child, serializers.Serializer)
self.has_context = isinstance(field, serializers.Serializer) or self.is_many
if self.has_context:
self.old_context = None
self.only_fields = self.filter_fields(field.field_name, only_fields)
self.include_fields = self.filter_fields(field.field_name, include_fields)
self.on_exit_delete_fields = False
self.on_exit_delete_include_fields = False
self.old_fields = None
self.old_include_fields = None
示例6: __new__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def __new__(cls, *args, **kwargs):
"""
Custom constructor that sets the ListSerializer to
DynamicListSerializer to avoid re-evaluating querysets.
Addresses DRF 3.1.0 bug:
https://github.com/tomchristie/django-rest-framework/issues/2704
"""
meta = getattr(cls, 'Meta', None)
if not meta:
meta = type('Meta', (), {})
cls.Meta = meta
list_serializer_class = getattr(
meta,
'list_serializer_class',
settings.LIST_SERIALIZER_CLASS or DynamicListSerializer
)
if not issubclass(list_serializer_class, DynamicListSerializer):
list_serializer_class = DynamicListSerializer
meta.list_serializer_class = list_serializer_class
return super(
WithDynamicSerializerMixin, cls
).__new__(
cls, *args, **kwargs
)
示例7: get_field_info
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def get_field_info(self, field):
"""Adds `related_to` and `nullable` to the metadata response."""
field_info = OrderedDict()
for attr in ('required', 'read_only', 'default', 'label'):
field_info[attr] = getattr(field, attr)
if field_info['default'] is empty:
field_info['default'] = None
if hasattr(field, 'immutable'):
field_info['immutable'] = field.immutable
field_info['nullable'] = field.allow_null
if hasattr(field, 'choices'):
field_info['choices'] = [
{
'value': choice_value,
'display_name': force_text(choice_name, strings_only=True)
}
for choice_value, choice_name in field.choices.items()
]
many = False
if isinstance(field, DynamicRelationField):
field = field.serializer
if isinstance(field, ListSerializer):
field = field.child
many = True
if isinstance(field, ModelSerializer):
type = 'many' if many else 'one'
field_info['related_to'] = field.get_plural_name()
else:
type = self.label_lookup[field]
field_info['type'] = type
return field_info
示例8: get_related_field
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def get_related_field(field):
if isinstance(field, ManyRelatedField):
return field.child_relation
if isinstance(field, ListSerializer):
return field.child
return field
示例9: is_related_many
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def is_related_many(field):
if hasattr(field, "many"):
return field.many
if isinstance(field, ManyRelatedField):
return True
if isinstance(field, ListSerializer):
return True
return False
示例10: data
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def data(self):
return super(serializers.ListSerializer, self).data
示例11: data
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def data(self):
# Note we deliberately return the super of ListSerializer to avoid
# instantiating a ReturnList, which would force evaluating the generator
return super(serializers.ListSerializer, self).data
# --------------------------------------------------------
# Document Note Views
# --------------------------------------------------------
示例12: data
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def data(self):
ret = super(serializers.ListSerializer, self).data
return ReturnDict(ret, serializer=self)
示例13: list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def list(self, request):
"""
Returns all the beer styles. Do not show suggested
beer styles by default.
"""
styles = models.Style.objects.all()
if request.auth is None:
styles = styles[:settings.UNAUTHENTICATED_RESULTS_COUNT]
serializer = rf_serializers.ListSerializer(
styles,
child=serializers.SimpleStyleSerializer()
)
return Response(serializer.data)
示例14: list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def list(self, request):
"""
Returns all hop types in the system
"""
serializer = rf_serializers.ListSerializer(
models.HopType.objects.all() if request.auth is not None else models.HopType.objects.all()[:settings.UNAUTHENTICATED_RESULTS_COUNT],
child=serializers.HopTypeSerializer()
)
return Response(serializer.data)
示例15: list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ListSerializer [as 别名]
def list(self, request):
"""
Returns all countries in the system.
"""
serializer = rf_serializers.ListSerializer(
models.CountryCode.objects.all() if request.auth is not None else models.CountryCode.objects.all()[:settings.UNAUTHENTICATED_RESULTS_COUNT],
child=serializers.CountryCodeSerializer()
)
return Response(serializer.data)