本文整理汇总了Python中rest_framework.serializers.SerializerMethodField方法的典型用法代码示例。如果您正苦于以下问题:Python serializers.SerializerMethodField方法的具体用法?Python serializers.SerializerMethodField怎么用?Python serializers.SerializerMethodField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.serializers
的用法示例。
在下文中一共展示了serializers.SerializerMethodField方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_fields
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def get_fields(self):
"""
This returns a dictionary containing the top most fields,
``dates``, ``fields`` and ``queries``.
"""
field_mapping = OrderedDict()
for field, data in self.instance.items():
field_mapping.update(
{field: self.facet_dict_field_class(
child=self.facet_list_field_class(child=self.facet_field_serializer_class(data)), required=False)}
)
if self.serialize_objects is True:
field_mapping["objects"] = serializers.SerializerMethodField()
return field_mapping
示例2: validate
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def validate(self, attrs):
# Because `partial_permissions` is a `SerializerMethodField`,
# it's read-only, so it's not validated nor added to `validated_data`.
# We need to do it manually
self.validate_partial_permissions(attrs)
return attrs
示例3: __init__
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def __init__(self, *args, **kwargs):
self._serializer_method_field = serializers.SerializerMethodField()
super().__init__(*args, **kwargs)
示例4: compat_serializer_method_field
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def compat_serializer_method_field(method_name=None):
""" method_name changed in DRF > 3. See http://www.django-rest-framework.org/topics/3.0-announcement/#optional-argument-to-serializermethodfield. """
if DRFVLIST[0] >= 3:
return serializers.SerializerMethodField()
else:
return serializers.SerializerMethodField(method_name=method_name)
示例5: perform_mutate
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def perform_mutate(cls, serializer, info):
obj = serializer.save()
kwargs = {}
for f, field in serializer.fields.items():
if not field.write_only:
if isinstance(field, serializers.SerializerMethodField):
kwargs[f] = field.to_representation(obj)
else:
kwargs[f] = field.get_attribute(obj)
return cls(errors=None, **kwargs)
示例6: get_fields
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def get_fields(self):
field_mapping = OrderedDict()
field_mapping["facets"] = SerializerMethodField()
if self.serialize_objects is True:
field_mapping["objects"] = SerializerMethodField()
return field_mapping
示例7: _get_field_type
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def _get_field_type(serializer, field_name, field, include_read_only):
"""
Try to describe a field type.
"""
if not include_read_only and hasattr(field, 'writable_doc_format'):
return _get_type_from_docstring(field.writable_doc_format)
if hasattr(field, 'doc_format'):
return _get_type_from_docstring(field.doc_format)
if isinstance(field, (relations.ManyRelatedField, serializers.ListSerializer)):
# Many field, recurse on child and make it a list
if isinstance(field, relations.ManyRelatedField):
field = field.child_relation
else:
field = field.child
return [_get_field_type(serializer, field_name, field, include_read_only)]
if field.__class__.__name__ in _SERIALIZER_DEFS:
return _SERIALIZER_DEFS[field.__class__.__name__]
if isinstance(field, serializers.SlugRelatedField):
return _get_details_for_slug(serializer, field_name, field)
if isinstance(field, serializers.SerializerMethodField):
# For method fields try to use docstring of the method.
method_name = field.method_name or 'get_{field_name}'.format(field_name=field_name)
method = getattr(serializer, method_name, None)
if method:
docstring = getattr(method, '__doc__')
return _get_type_from_docstring(docstring, docstring or 'method')
if isinstance(field, serializers.BaseSerializer):
return describe_serializer(field, include_read_only)
logger = logging.getLogger(__name__)
logger.error('Undocumented field %s' % field)
return 'UNKNOWN'
开发者ID:product-definition-center,项目名称:product-definition-center,代码行数:40,代码来源:renderers_serializers.py
示例8: setUp
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import SerializerMethodField [as 别名]
def setUp(self):
MockPersonIndex().reindex()
MockPetIndex().reindex()
class Serializer1(HaystackSerializer):
integer_field = serializers.IntegerField()
city = serializers.SerializerMethodField()
class Meta:
index_classes = [MockPersonIndex]
fields = ["text", "firstname", "lastname", "autocomplete"]
def get_integer_field(self, instance):
return 1
def get_city(self, instance):
return "Declared overriding field"
class Serializer2(HaystackSerializer):
class Meta:
index_classes = [MockPersonIndex]
exclude = ["firstname"]
class Serializer3(HaystackSerializer):
class Meta:
index_classes = [MockPersonIndex]
fields = ["text", "firstname", "lastname", "autocomplete"]
ignore_fields = ["autocomplete"]
class Serializer7(HaystackSerializer):
class Meta:
index_classes = [MockPetIndex]
class ViewSet1(HaystackViewSet):
serializer_class = Serializer1
class Meta:
index_models = [MockPerson]
self.serializer1 = Serializer1
self.serializer2 = Serializer2
self.serializer3 = Serializer3
self.serializer7 = Serializer7
self.view1 = ViewSet1