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


Python fields.SkipField方法代碼示例

本文整理匯總了Python中rest_framework.fields.SkipField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.SkipField方法的具體用法?Python fields.SkipField怎麽用?Python fields.SkipField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rest_framework.fields的用法示例。


在下文中一共展示了fields.SkipField方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_attribute

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def get_attribute(self, instance):
        obj = super(CurrentProjectRelatedField, self).get_attribute(instance)

        is_art = (
            self.field_name == 'current_art_project' and
            isinstance(obj, ArtProject)
        )
        is_res = (
            self.field_name == 'current_research_project' and
            isinstance(obj, ResearchProject)
        )

        if is_art or is_res:
            return obj

        raise drf_fields.SkipField() 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:18,代碼來源:serializers.py

示例2: test_single_hyperlinked_related_field

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def test_single_hyperlinked_related_field(self):
        field = HyperlinkedRelatedField(
            related_link_view_name='entry-blog',
            related_link_url_kwarg='entry_pk',
            self_link_view_name='entry-relationships',
            read_only=True,
        )
        field._context = {'request': self.request, 'view': self.view}
        field.field_name = 'blog'

        self.assertRaises(NotImplementedError, field.to_representation, self.entry)
        self.assertRaises(SkipField, field.get_attribute, self.entry)

        links_expected = {
            'self': 'http://testserver/entries/{}/relationships/blog'.format(self.entry.pk),
            'related': 'http://testserver/entries/{}/blog'.format(self.entry.pk)
        }
        got = field.get_links(self.entry)
        self.assertEqual(got, links_expected) 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:21,代碼來源:test_relations.py

示例3: test_many_hyperlinked_related_field

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def test_many_hyperlinked_related_field(self):
        field = HyperlinkedRelatedField(
            related_link_view_name='entry-comments',
            related_link_url_kwarg='entry_pk',
            self_link_view_name='entry-relationships',
            read_only=True,
            many=True
        )
        field._context = {'request': self.request, 'view': self.view}
        field.field_name = 'comments'

        self.assertRaises(NotImplementedError, field.to_representation, self.entry.comments.all())
        self.assertRaises(SkipField, field.get_attribute, self.entry)

        links_expected = {
            'self': 'http://testserver/entries/{}/relationships/comments'.format(self.entry.pk),
            'related': 'http://testserver/entries/{}/comments'.format(self.entry.pk)
        }
        got = field.child_relation.get_links(self.entry)
        self.assertEqual(got, links_expected) 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:22,代碼來源:test_relations.py

示例4: test_single_serializer_method_hyperlinked_related_field

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def test_single_serializer_method_hyperlinked_related_field(self):
        serializer = EntryModelSerializerWithHyperLinks(
            instance=self.entry,
            context={
                'request': self.request,
                'view': self.view
            }
        )
        field = serializer.fields['blog']

        self.assertRaises(NotImplementedError, field.to_representation, self.entry)
        self.assertRaises(SkipField, field.get_attribute, self.entry)

        expected = {
            'self': 'http://testserver/entries/{}/relationships/blog'.format(self.entry.pk),
            'related': 'http://testserver/entries/{}/blog'.format(self.entry.pk)
        }
        got = field.get_links(self.entry)
        self.assertEqual(got, expected) 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:21,代碼來源:test_relations.py

示例5: test_many_serializer_method_hyperlinked_related_field

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def test_many_serializer_method_hyperlinked_related_field(self):
        serializer = EntryModelSerializerWithHyperLinks(
            instance=self.entry,
            context={
                'request': self.request,
                'view': self.view
            }
        )
        field = serializer.fields['comments']

        self.assertRaises(NotImplementedError, field.to_representation, self.entry)
        self.assertRaises(SkipField, field.get_attribute, self.entry)

        expected = {
            'self': 'http://testserver/entries/{}/relationships/comments'.format(self.entry.pk),
            'related': 'http://testserver/entries/{}/comments'.format(self.entry.pk)
        }
        got = field.get_links(self.entry)
        self.assertEqual(got, expected) 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:21,代碼來源:test_relations.py

示例6: to_representation

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def to_representation(self, instance):
        """
        Object instance -> Dict of primitive datatypes.
        """
        ret = OrderedDict()
        fields = [field for field in self.fields.values() if not field.write_only]

        for field in fields:
            try:
                attribute = field.get_attribute(instance)
            except SkipField:
                continue

            if attribute is not None:
                represenation = field.to_representation(attribute)
                if represenation is None:
                    # Do not seralize empty objects
                    continue
                if isinstance(represenation, list) and not represenation:
                   # Do not serialize empty lists
                   continue
                ret[field.field_name] = represenation

        return ret 
開發者ID:PerchLive,項目名稱:django-broadcast,代碼行數:26,代碼來源:serializers.py

示例7: _get_dynamic_data

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def _get_dynamic_data(self, validated_data):
        """
        Returns dict of data, not declared in serializer fields.
        Should be called after self.is_valid().
        """
        result = {}

        for key in self.initial_data:
            if key not in validated_data:
                try:
                    field = self.fields[key]
                    # no exception? this is either SkipField or error
                    # in particular, this might be a read-only field
                    # that was mistakingly given a value
                    if not isinstance(field, drf_fields.SkipField):
                        msg = (
                            'Field %s is missing from validated data,'
                            'but is not a SkipField!'
                        ) % key
                        raise AssertionError(msg)
                except KeyError:  # ok, this is dynamic data
                    result[key] = self.initial_data[key]
        return result 
開發者ID:umutbozkurt,項目名稱:django-rest-framework-mongoengine,代碼行數:25,代碼來源:serializers.py

示例8: __call__

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def __call__(self, attrs, serializer):
        try:
            self.enforce_required_fields(attrs, serializer)
        except SkipField:
            return

        # Determine the existing instance, if this is an update operation.
        instance = getattr(serializer, 'instance', None)

        queryset = self.queryset
        queryset = self.filter_queryset(attrs, queryset, serializer)
        queryset = self.exclude_current_instance(queryset, instance)

        # Ignore validation if any field is None
        checked_values = [
            value for field, value in attrs.items() if field in self.fields
        ]
        if None not in checked_values and queryset.first():
            field_names = ', '.join(self.fields)
            raise ValidationError(self.message.format(field_names=field_names)) 
開發者ID:umutbozkurt,項目名稱:django-rest-framework-mongoengine,代碼行數:22,代碼來源:validators.py

示例9: get_attribute

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def get_attribute(self, instance):
        # When handling the create() all, skip this field.
        if isinstance(instance, (dict, OrderedDict)):
            raise SkipField()

        assert isinstance(instance, TranslatedFieldsModel), (
            ("The TranslatedAbsoluteUrlField can only be used on a TranslatableModelSerializer, "
             " not on a {0}".format(instance.__class__))
        )

        return instance 
開發者ID:django-parler,項目名稱:django-parler-rest,代碼行數:13,代碼來源:fields.py

示例10: get_attribute

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def get_attribute(self, instance):
        try:
            instance.pk
        except AttributeError:
            raise SkipField()
        return super().get_attribute(instance) 
開發者ID:OpenAgricultureFoundation,項目名稱:gro-api,代碼行數:8,代碼來源:serializers.py

示例11: to_internal_value

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def to_internal_value(self, data):
        if isinstance(data, dict) and 'name' in data and data['name'] is None:
            raise SkipField()
        return super(ReleaseComponentSRPMNameMappingNestedSerializer, self).to_internal_value(data) 
開發者ID:product-definition-center,項目名稱:product-definition-center,代碼行數:6,代碼來源:serializers.py

示例12: enforce_required_fields

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def enforce_required_fields(self, attrs, serializer):
        try:
            super(OptionalUniqueTogetherValidator, self).enforce_required_fields(attrs, serializer)
        except ValidationError as e:
            if set(e.detail.keys()) == set(self.fields):
                raise SkipField()
            else:
                raise 
開發者ID:umutbozkurt,項目名稱:django-rest-framework-mongoengine,代碼行數:10,代碼來源:validators.py

示例13: to_representation

# 需要導入模塊: from rest_framework import fields [as 別名]
# 或者: from rest_framework.fields import SkipField [as 別名]
def to_representation(self, instance):
        data = OrderedDict()
        fields = [field for field in self.fields.values() if not field.write_only]

        # Split meta fields from core fields
        meta_fields = [field for field in fields if field.field_name in self.meta_fields]
        fields = [field for field in fields if field.field_name not in self.meta_fields]

        # Make sure id is always first. This will be filled in later
        if 'id' in [field.field_name for field in fields]:
            data['id'] = None

        # Serialise meta fields
        meta = OrderedDict()
        for field in meta_fields:
            try:
                attribute = field.get_attribute(instance)
            except SkipField:
                continue

            if attribute is None:
                # We skip `to_representation` for `None` values so that
                # fields do not have to explicitly deal with that case.
                meta[field.field_name] = None
            else:
                meta[field.field_name] = field.to_representation(attribute)

        if meta:
            data['meta'] = meta

        # Serialise core fields
        for field in fields:
            try:
                attribute = field.get_attribute(instance)
            except SkipField:
                continue

            if attribute is None:
                # We skip `to_representation` for `None` values so that
                # fields do not have to explicitly deal with that case.
                data[field.field_name] = None
            else:
                data[field.field_name] = field.to_representation(attribute)

        return data 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:47,代碼來源:serializers.py


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