本文整理匯總了Python中rest_framework.serializers.Field方法的典型用法代碼示例。如果您正苦於以下問題:Python serializers.Field方法的具體用法?Python serializers.Field怎麽用?Python serializers.Field使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rest_framework.serializers
的用法示例。
在下文中一共展示了serializers.Field方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_embedding_custom_bottom
# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import Field [as 別名]
def test_embedding_custom_bottom(self):
class CustomEmbedding(Field):
bla = drf_fields.CharField()
class TestSerializer(DocumentSerializer):
serializer_embedded_bottom = CustomEmbedding
class Meta:
model = NestedEmbeddingDoc
fields = '__all__'
depth_embedding = 0
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = CustomEmbedding(default=None, required=False)
""")
assert repr(TestSerializer()) == expected
示例2: test_should_base_field_convert_string
# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import Field [as 別名]
def test_should_base_field_convert_string():
assert_conversion(serializers.Field, graphene.String)
示例3: test_should_model_convert_field
# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import Field [as 別名]
def test_should_model_convert_field():
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = None
fields = "__all__"
assert_conversion(MyModelSerializer, graphene.Field, is_input=False)
示例4: test_describe_field_with_custom_type
# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import Field [as 別名]
def test_describe_field_with_custom_type(self):
class DummyField(serializers.Field):
doc_format = '{"foo": "bar"}'
writable_doc_format = '{"baz": "quux"}'
class DummySerializer(serializers.Serializer):
field = DummyField()
instance = DummySerializer()
result = describe_serializer(instance, include_read_only=True)
self.assertEqual(_flatten_field_data(result), {'field': {'value': {'foo': 'bar'}}})
result = describe_serializer(instance, include_read_only=False)
self.assertEqual(_flatten_field_data(result), {'field': {'value': {'baz': 'quux'}}})
示例5: get_attribute
# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import Field [as 別名]
def get_attribute(self, instance):
return serializers.Field.get_attribute(self, instance)
示例6: modelserializer_factory
# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import Field [as 別名]
def modelserializer_factory(model, serializer=serializers.ModelSerializer, fields=None, exclude=None, **kwargs):
"""
Generate serializer basing on django's modelform_factory
:param model: model we create serializer for
:param serializer: base serializer class
:param fields: list of fields to include in serializer
:param exclude: list of fields to exclude from serializer
:param kwargs: fields mapping
:return:
"""
# TODO: decide if we need cache and what to do with parameters tha can be different
serializer_class = serializer_cache.get(model, None)
if serializer_class:
return serializer_class
def _get_declared_fields(attrs):
fields = [(field_name, attrs.pop(field_name))
for field_name, obj in list(attrs.items())
if isinstance(obj, serializers.Field)]
fields.sort(key=lambda x: x[1]._creation_counter)
return OrderedDict(fields)
meta_attrs = {'model': model}
if fields is not None:
meta_attrs['fields'] = fields
if exclude is not None:
meta_attrs['exclude'] = exclude
if fields is None and exclude is None:
meta_attrs['fields'] = '__all__'
parent = (object, )
Meta = type(str('Meta'), parent, meta_attrs)
class_name = model.__name__ + str('Serializer')
serializer_class_attrs = {
'Meta': Meta,
'_get_declared_fields': _get_declared_fields(kwargs),
}
serializer_class = type(serializer)(class_name, (serializer,), serializer_class_attrs)
serializer_cache[model] = serializer_class
return serializer_class