当前位置: 首页>>代码示例>>Python>>正文


Python serializers.ReadOnlyField方法代码示例

本文整理汇总了Python中rest_framework.serializers.ReadOnlyField方法的典型用法代码示例。如果您正苦于以下问题:Python serializers.ReadOnlyField方法的具体用法?Python serializers.ReadOnlyField怎么用?Python serializers.ReadOnlyField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rest_framework.serializers的用法示例。


在下文中一共展示了serializers.ReadOnlyField方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_fields

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def get_fields(self):
        fields = super(RasterModelSerializer, self).get_fields()
        if not self.Meta.raster_field:
            for name, field in fields.items():
                if isinstance(field, serializers.FileField):
                    self.Meta.raster_field = name
                    break
        fieldname = self.Meta.raster_field
        request = self.context.get('request')
        renderer = getattr(request, 'accepted_renderer', None)
        try:
            obj = self.instance[0]
        except (IndexError, TypeError):
            obj = self.instance
        modelfield = getattr(obj, fieldname, None)
        if (isinstance(renderer, BaseGDALRenderer)
                or not isinstance(modelfield, FieldFile)):
            fields[fieldname] = serializers.ReadOnlyField()
        return fields 
开发者ID:bkg,项目名称:django-spillway,代码行数:21,代码来源:serializers.py

示例2: test_method_field

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def test_method_field(self):
        """
        Properties and methods on the model should be allowed as `Meta.fields`
        values, and should map to `ReadOnlyField`.
        """

        class TestSerializer(DocumentSerializer):
            class Meta:
                model = RegularModel
                fields = ('id', 'method')

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                method = ReadOnlyField()
        """)
        assert repr(TestSerializer()) == expected 
开发者ID:umutbozkurt,项目名称:django-rest-framework-mongoengine,代码行数:19,代码来源:test_basic.py

示例3: test_missing_field

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def test_missing_field(self):
        """
        Fields that have been declared on the serializer class must be included
        in the `Meta.fields` if it exists.
        """

        class TestSerializer(DocumentSerializer):
            missing = serializers.ReadOnlyField()

            class Meta:
                model = RegularModel
                fields = ('id',)

        with pytest.raises(AssertionError) as exc:
            TestSerializer().fields
        expected = (
            "The field 'missing' was declared on serializer TestSerializer, "
            "but has not been included in the 'fields' option."
        )
        assert expected in str(exc.value) 
开发者ID:umutbozkurt,项目名称:django-rest-framework-mongoengine,代码行数:22,代码来源:test_basic.py

示例4: test_missing_superclass_field_not_included

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def test_missing_superclass_field_not_included(self):
        """
        Fields that have been declared on a parent of the serializer class may
        be excluded from the `Meta.fields` option.
        """

        class TestSerializer(DocumentSerializer):
            missing = serializers.ReadOnlyField()

            class Meta:
                model = RegularModel
                fields = '__all__'

        class ChildSerializer(TestSerializer):
            missing = serializers.ReadOnlyField()

            class Meta:
                model = RegularModel
                fields = ('id',)

        ChildSerializer().fields 
开发者ID:umutbozkurt,项目名称:django-rest-framework-mongoengine,代码行数:23,代码来源:test_basic.py

示例5: test_missing_superclass_field_excluded

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def test_missing_superclass_field_excluded(self):
        """
        Fields that have been declared on a parent of the serializer class may
        be excluded from the `Meta.fields` option.
        """

        class TestSerializer(DocumentSerializer):
            missing = serializers.ReadOnlyField()

            class Meta:
                model = RegularModel
                fields = '__all__'

        class ChildSerializer(TestSerializer):
            missing = serializers.ReadOnlyField()

            class Meta:
                model = RegularModel
                exclude = ('missing',)

        ChildSerializer().fields 
开发者ID:umutbozkurt,项目名称:django-rest-framework-mongoengine,代码行数:23,代码来源:test_basic.py

示例6: test_choices_with_nonstandard_args

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def test_choices_with_nonstandard_args(self):
        class TestSerializer(DocumentSerializer):
            missing = serializers.ReadOnlyField()

            class Meta:
                model = ComplexChoicesModel
                fields = '__all__'

        TestSerializer().fields 
开发者ID:umutbozkurt,项目名称:django-rest-framework-mongoengine,代码行数:11,代码来源:test_basic.py

示例7: serializer_factory

# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def serializer_factory(endpoint=None, fields=None, base_class=None, model=None):

    if model is not None:
        assert endpoint is None, "You cannot specify both a model and an endpoint"
        from .endpoints import Endpoint
        endpoint = Endpoint(model=model)
    else:
        assert endpoint is not None, "You have to specify either a model or an endpoint"

    if base_class is None:
        base_class = endpoint.base_serializer

    meta_attrs = {
        'model': endpoint.model,
        'fields': fields if fields is not None else endpoint.get_fields_for_serializer()
    }
    meta_parents = (object, )
    if hasattr(base_class, 'Meta'):
        meta_parents = (base_class.Meta, ) + meta_parents

    Meta = type('Meta', meta_parents, meta_attrs)

    cls_name = '{}Serializer'.format(endpoint.model.__name__)
    cls_attrs = {
        'Meta': Meta,
    }

    for meta_field in meta_attrs['fields']:
        if meta_field not in base_class._declared_fields:
            try:
                model_field = endpoint.model._meta.get_field(meta_field)
                if isinstance(model_field, OneToOneRel):
                    cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(read_only=True)
                elif isinstance(model_field, ManyToOneRel):
                    cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
                elif isinstance(model_field, ManyToManyRel):
                    # related ManyToMany should not be required
                    cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(
                        many=True,
                        required=False,
                        queryset=model_field.related_model.objects.all()
                    )
            except FieldDoesNotExist:
                cls_attrs[meta_field] = serializers.ReadOnlyField()

    try:
        return type(cls_name, (NullToDefaultMixin, base_class, ), cls_attrs)
    except TypeError:
        # MRO issue, let's try the other way around
        return type(cls_name, (base_class, NullToDefaultMixin, ), cls_attrs) 
开发者ID:drf-forms,项目名称:drf-schema-adapter,代码行数:52,代码来源:factories.py


注:本文中的rest_framework.serializers.ReadOnlyField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。