Python rest_framework.fields.SkipField() Examples
The following are 13
code examples of rest_framework.fields.SkipField().
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 also want to check out all available functions/classes of the module
rest_framework.fields
, or try the search function
.
Example #1
Source File: serializers.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 6 votes |
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()
Example #2
Source File: test_relations.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #3
Source File: test_relations.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #4
Source File: test_relations.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #5
Source File: test_relations.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #6
Source File: serializers.py From django-broadcast with Apache License 2.0 | 6 votes |
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
Example #7
Source File: serializers.py From django-rest-framework-mongoengine with MIT License | 6 votes |
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
Example #8
Source File: validators.py From django-rest-framework-mongoengine with MIT License | 6 votes |
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))
Example #9
Source File: fields.py From django-parler-rest with Apache License 2.0 | 5 votes |
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
Example #10
Source File: serializers.py From gro-api with GNU General Public License v2.0 | 5 votes |
def get_attribute(self, instance): try: instance.pk except AttributeError: raise SkipField() return super().get_attribute(instance)
Example #11
Source File: serializers.py From product-definition-center with MIT License | 5 votes |
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)
Example #12
Source File: validators.py From django-rest-framework-mongoengine with MIT License | 5 votes |
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
Example #13
Source File: serializers.py From wagtail with BSD 3-Clause "New" or "Revised" License | 4 votes |
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