Python rest_framework.serializers.Field() Examples

The following are 6 code examples for showing how to use rest_framework.serializers.Field(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module rest_framework.serializers , or try the search function .

Example 1
Project: django-rest-framework-mongoengine   Author: umutbozkurt   File: test_embedded.py    License: MIT License 6 votes vote down vote up
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 
Example 2
Project: graphene-django   Author: graphql-python   File: test_field_converter.py    License: MIT License 5 votes vote down vote up
def test_should_base_field_convert_string():
    assert_conversion(serializers.Field, graphene.String) 
Example 3
Project: graphene-django   Author: graphql-python   File: test_field_converter.py    License: MIT License 5 votes vote down vote up
def test_should_model_convert_field():
    class MyModelSerializer(serializers.ModelSerializer):
        class Meta:
            model = None
            fields = "__all__"

    assert_conversion(MyModelSerializer, graphene.Field, is_input=False) 
Example 4
Project: product-definition-center   Author: product-definition-center   File: tests.py    License: MIT License 5 votes vote down vote up
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'}}}) 
Example 5
Project: django-rest-framework-mongoengine   Author: umutbozkurt   File: fields.py    License: MIT License 5 votes vote down vote up
def get_attribute(self, instance):
        return serializers.Field.get_attribute(self, instance) 
Example 6
Project: djangocms-rest-api   Author: divio   File: base.py    License: MIT License 4 votes vote down vote up
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