當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。