本文整理汇总了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()
示例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)
示例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)
示例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)
示例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)
示例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
示例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
示例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))
示例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
示例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)
示例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)
示例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
示例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