本文整理汇总了Python中rest_framework.fields.get_component函数的典型用法代码示例。如果您正苦于以下问题:Python get_component函数的具体用法?Python get_component怎么用?Python get_component使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_component函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: field_to_native
def field_to_native(self, obj, field_name):
if not hasattr(self, 'context') or not 'request' in self.context:
raise ImproperlyConfigured('Pass request in self.context when'
' using CollectionMembershipField.')
request = self.context['request']
# Having 'use-es-for-apps' in the context means the parent view wants us
# to use ES to fetch the apps. If that key is present, check that we
# have a view in the context and that the waffle flag is active. If
# everything checks out, bypass the db and use ES to fetch apps for a
# nice performance boost.
if ('use-es-for-apps' in self.context and 'view' in self.context
and waffle.switch_is_active('collections-use-es-for-apps')):
return self.field_to_native_es(obj, request)
qs = get_component(obj, self.source)
# Filter apps based on feature profiles.
profile = get_feature_profile(request)
if profile:
qs = qs.filter(**profile.to_kwargs(
prefix='_current_version__features__has_'))
return [self.to_native(app) for app in qs]
开发者ID:hardikj,项目名称:zamboni,代码行数:25,代码来源:serializers.py
示例2: field_to_native
def field_to_native(self, obj, field_name):
try:
if self.source == '*':
return self.to_native(obj)
source = self.source or field_name
value = obj
for component in source.split('.'):
if value is None:
break
value = get_component(value, component)
except ObjectDoesNotExist:
return None
if value is None:
return None
if self.many:
if is_simple_callable(getattr(value, 'all', None)):
return [self.to_native(item) for item in value.all()]
else:
# Also support non-queryset iterables.
# This allows us to also support plain lists of related items.
return [self.to_native(item) for item in value]
return self.to_native(value)
开发者ID:9gix,项目名称:django-rest-framework,代码行数:26,代码来源:relations.py
示例3: field_to_native
def field_to_native(self, obj, field_name):
if not hasattr(self, 'context') or not 'request' in self.context:
raise ImproperlyConfigured('Pass request in self.context when'
' using CollectionMembershipField.')
request = self.context['request']
# Having 'use-es-for-apps' in the context means the parent view wants
# us to use ES to fetch the apps. If that key is present, check that we
# have a view in the context and that the waffle flag is active. If
# everything checks out, bypass the db and use ES to fetch apps for a
# nice performance boost.
if self.context.get('use-es-for-apps') and self.context.get('view'):
return self.field_to_native_es(obj, request)
qs = get_component(obj, self.source)
# Filter apps based on device and feature profiles.
device = self._get_device(request)
profile = get_feature_profile(request)
if device and device != amo.DEVICE_DESKTOP:
qs = qs.filter(addondevicetype__device_type=device.id)
if profile:
qs = qs.filter(**profile.to_kwargs(
prefix='_current_version__features__has_'))
return self.to_native(qs)
开发者ID:anushbmx,项目名称:zamboni,代码行数:27,代码来源:serializers.py
示例4: field_to_native
def field_to_native(self, obj, field_name):
if self.many:
# To-many relationship
queryset = None
if not self.source:
# Prefer obj.serializable_value for performance reasons
try:
queryset = obj.serializable_value(field_name)
except AttributeError:
pass
if queryset is None:
# RelatedManager (reverse relationship)
source = self.source or field_name
queryset = obj
for component in source.split('.'):
queryset = get_component(queryset, component)
# Forward relationship
return [self.to_native(item.pk) for item in queryset.all()]
# To-one relationship
try:
# Prefer obj.serializable_value for performance reasons
pk = obj.serializable_value(self.source or field_name)
except AttributeError:
# RelatedObject (reverse relationship)
try:
pk = getattr(obj, self.source or field_name).pk
except ObjectDoesNotExist:
return None
# Forward relationship
return self.to_native(pk)
开发者ID:watc,项目名称:django-rest-framework,代码行数:34,代码来源:relations.py
示例5: field_to_native
def field_to_native(self, obj, field_name):
source = self.source or field_name
value = obj
for component in source.split('.'):
value = fields.get_component(value, component)
if value is None:
break
field = value
if field is None:
return None
if self.requested_language:
return self.fetch_single_translation(obj, source, field)
else:
return self.fetch_all_translations(obj, source, field)
开发者ID:Jobava,项目名称:zamboni,代码行数:15,代码来源:fields.py
示例6: field_to_native
def field_to_native(self, obj, field_name):
value = get_component(obj, self.source)
# Filter apps based on feature profiles.
if hasattr(self, 'context') and 'request' in self.context:
sig = self.context['request'].GET.get('pro')
if sig:
try:
profile = FeatureProfile.from_signature(sig)
except ValueError:
pass
else:
value = value.filter(**profile.to_kwargs(
prefix='app___current_version__features__has_'))
return [self.to_native(item) for item in value.all()]
开发者ID:rhelmer,项目名称:zamboni,代码行数:16,代码来源:serializers.py
示例7: field_to_native
def field_to_native(self, obj, field_name):
if not hasattr(self, "context") or not "request" in self.context:
raise ImproperlyConfigured("Pass request in self.context when" " using CollectionMembershipField.")
request = self.context["request"]
# If we have a search resource in the context, we should try to use ES
# to fetch the apps, if the waffle flag is active.
if "search_resource" in self.context and waffle.switch_is_active("collections-use-es-for-apps"):
return self.field_to_native_es(obj, request)
value = get_component(obj, self.source)
# Filter apps based on feature profiles.
profile = get_feature_profile(request)
if profile and waffle.switch_is_active("buchets"):
value = value.filter(**profile.to_kwargs(prefix="app___current_version__features__has_"))
return [self.to_native(item) for item in value.all()]
开发者ID:pombredanne,项目名称:zamboni,代码行数:19,代码来源:serializers.py
示例8: field_to_native
def field_to_native(self, obj, field_name):
if obj is None:
return self.empty
request = self.context['request']
try:
state = obj.user_states.get(user=request.user)
except UserEntryState.DoesNotExist:
return self.to_native(False)
else:
source = self.source or field_name
value = state
for component in source.split('.'):
value = fields.get_component(state, component)
if value is None:
break
return self.to_native(value)
开发者ID:yhlam,项目名称:cobble,代码行数:20,代码来源:serializers.py
示例9: field_to_native
def field_to_native(self, obj, field_name):
try:
if self.source == '*':
return self.to_native(obj)
source = self.source or field_name
value = obj
for component in source.split('.'):
value = get_component(value, component)
if value is None:
break
except ObjectDoesNotExist:
return None
if value is None:
return None
if self.many:
return [self.to_native(item) for item in value.all()]
return self.to_native(value)
开发者ID:derega,项目名称:django-rest-framework,代码行数:21,代码来源:relations.py
示例10: field_to_native
def field_to_native(self, obj, field_name):
"""
Override default so that the serializer can be used as a nested field
across relationships.
"""
if self.write_only:
return None
if self.source == '*':
return self.to_native(obj)
# Get the raw field value
try:
source = self.source or field_name
value = obj
for component in source.split('.'):
if value is None:
break
value = get_component(value, component)
except ObjectDoesNotExist:
return None
if is_simple_callable(getattr(value, 'all', None)):
filtered_queryset = value.filter(self.get_related_filter())
return [self.to_native(item) for item in filtered_queryset]
if value is None:
return None
if self.many is not None:
many = self.many
else:
many = hasattr(value, '__iter__') and not isinstance(value, (Page, dict, six.text_type))
if many:
return [self.to_native(item) for item in value]
return self.to_native(value)
开发者ID:codeforeurope,项目名称:stadtgedaechtnis_backend,代码行数:38,代码来源:serializers.py
示例11: field_to_native
def field_to_native(self, obj, field_name):
"""
Get a value for serialization from an object.
This is mostly copy/paste from ``fields.Field``.
:args obj: The object to reead the value from.
:args field_name: The name of the field to read from obj.
"""
if obj is None:
return self.empty
if self.read_source == '*':
return self.to_native(obj)
source = self.read_source or field_name
value = obj
for component in source.split('.'):
value = get_component(value, component)
if value is None:
break
return self.to_native(value)
开发者ID:MarkSchmidty,项目名称:kitsune,代码行数:24,代码来源:api.py
注:本文中的rest_framework.fields.get_component函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论