本文整理匯總了Python中rest_framework.utils.serializer_helpers.ReturnList方法的典型用法代碼示例。如果您正苦於以下問題:Python serializer_helpers.ReturnList方法的具體用法?Python serializer_helpers.ReturnList怎麽用?Python serializer_helpers.ReturnList使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rest_framework.utils.serializer_helpers
的用法示例。
在下文中一共展示了serializer_helpers.ReturnList方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _run_test
# 需要導入模塊: from rest_framework.utils import serializer_helpers [as 別名]
# 或者: from rest_framework.utils.serializer_helpers import ReturnList [as 別名]
def _run_test(serializer_cls, model_cls, sql_queries=1) -> ReturnList:
"""
Boilerplate for running the tests
:return: the serializer data to assert one
"""
print(
f'Running test with serializer "{serializer_cls.__name__}" and model {model_cls.__name__}'
)
case = TestCase()
request = APIRequestFactory().get("/FOO")
with case.assertNumQueries(sql_queries):
prefetched_queryset = prefetch(model_cls.objects.all(), serializer_cls)
serializer_instance = serializer_cls(
instance=prefetched_queryset, many=True, context={"request": request}
)
print("Data returned:")
pprint_result(serializer_instance.data)
return serializer_instance.data
示例2: _get_error_details
# 需要導入模塊: from rest_framework.utils import serializer_helpers [as 別名]
# 或者: from rest_framework.utils.serializer_helpers import ReturnList [as 別名]
def _get_error_details(data, default_code=None):
"""
Descend into a nested data structure, forcing any
lazy translation strings or strings into `ErrorDetail`.
"""
if isinstance(data, list):
ret = [
_get_error_details(item, default_code) for item in data
]
if isinstance(data, ReturnList):
return ReturnList(ret, serializer=data.serializer)
return ret
elif isinstance(data, dict):
ret = {
key: _get_error_details(value, default_code)
for key, value in data.items()
}
if isinstance(data, ReturnDict):
return ReturnDict(ret, serializer=data.serializer)
return ret
text = force_text(data)
code = getattr(data, 'code', default_code)
return ErrorDetail(text, code)
示例3: data
# 需要導入模塊: from rest_framework.utils import serializer_helpers [as 別名]
# 或者: from rest_framework.utils.serializer_helpers import ReturnList [as 別名]
def data(self):
"""Get the data, after performing post-processing if necessary."""
data = super(DynamicListSerializer, self).data
processed_data = ReturnDict(
SideloadingProcessor(self, data).data,
serializer=self
) if self.child.envelope else ReturnList(
data,
serializer=self
)
processed_data = post_process(processed_data)
return processed_data
示例4: wrap_paginated
# 需要導入模塊: from rest_framework.utils import serializer_helpers [as 別名]
# 或者: from rest_framework.utils.serializer_helpers import ReturnList [as 別名]
def wrap_paginated(self, data, renderer_context):
"""Convert paginated data to JSON API with meta"""
pagination_keys = ['count', 'next', 'previous', 'results']
for key in pagination_keys:
if not (data and key in data):
raise WrapperNotApplicable('Not paginated results')
view = renderer_context.get("view", None)
model = self.model_from_obj(view)
resource_type = self.model_to_resource_type(model)
try:
from rest_framework.utils.serializer_helpers import ReturnList
results = ReturnList(
data["results"],
serializer=data.serializer.fields["results"],
)
except ImportError:
results = data["results"]
# Use default wrapper for results
wrapper = self.wrap_default(results, renderer_context)
# Add pagination metadata
pagination = self.dict_class()
pagination['previous'] = data['previous']
pagination['next'] = data['next']
pagination['count'] = data['count']
wrapper.setdefault('meta', self.dict_class())
wrapper['meta'].setdefault('pagination', self.dict_class())
wrapper['meta']['pagination'].setdefault(
resource_type, self.dict_class()).update(pagination)
return wrapper
示例5: pprint_result
# 需要導入模塊: from rest_framework.utils import serializer_helpers [as 別名]
# 或者: from rest_framework.utils.serializer_helpers import ReturnList [as 別名]
def pprint_result(list: ReturnList):
# Convert to regular dicts
def to_dict(input_ordered_dict):
return loads(dumps(input_ordered_dict))
pprint([to_dict(res) for res in list])