當前位置: 首頁>>代碼示例>>Python>>正文


Python serializers.Field方法代碼示例

本文整理匯總了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 
開發者ID:umutbozkurt,項目名稱:django-rest-framework-mongoengine,代碼行數:20,代碼來源:test_embedded.py

示例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) 
開發者ID:graphql-python,項目名稱:graphene-django,代碼行數:4,代碼來源:test_field_converter.py

示例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) 
開發者ID:graphql-python,項目名稱:graphene-django,代碼行數:9,代碼來源:test_field_converter.py

示例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'}}}) 
開發者ID:product-definition-center,項目名稱:product-definition-center,代碼行數:17,代碼來源:tests.py

示例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) 
開發者ID:umutbozkurt,項目名稱:django-rest-framework-mongoengine,代碼行數:4,代碼來源:fields.py

示例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 
開發者ID:divio,項目名稱:djangocms-rest-api,代碼行數:45,代碼來源:base.py


注:本文中的rest_framework.serializers.Field方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。