本文整理汇总了Python中django.utils.encoding.is_protected_type方法的典型用法代码示例。如果您正苦于以下问题:Python encoding.is_protected_type方法的具体用法?Python encoding.is_protected_type怎么用?Python encoding.is_protected_type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.encoding
的用法示例。
在下文中一共展示了encoding.is_protected_type方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_field_value
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def get_field_value(field, model):
if field.remote_field is None:
value = field.pre_save(model, add=model.pk is None)
# Make datetimes timezone aware
# https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1394-L1403
if isinstance(value, datetime.datetime) and settings.USE_TZ:
if timezone.is_naive(value):
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone).astimezone(timezone.utc)
# convert to UTC
value = timezone.localtime(value, timezone.utc)
if is_protected_type(value):
return value
else:
return field.value_to_string(model)
else:
return getattr(model, field.get_attname())
示例2: to_native
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def to_native(self, value):
"""
Converts the field's value into it's simple representation.
"""
if is_simple_callable(value):
value = value()
if is_protected_type(value):
return value
elif is_non_str_iterable(value) and not isinstance(value, (dict, six.string_types)):
return [self.to_native(item) for item in value]
elif isinstance(value, dict):
# Make sure we preserve field ordering, if it exists
ret = collections.OrderedDict()
for key, val in value.items():
ret[key] = self.to_native(val)
return ret
return force_text(value)
示例3: handle_field
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def handle_field(self, obj, field):
value = field._get_val_from_obj(obj)
# Protected types (i.e., primitives like None, numbers, dates,
# and Decimals) are passed through as is. All other values are
# converted to string first.
if is_protected_type(value):
self._current[field.name] = value
else:
self._current[field.name] = field.value_to_string(obj)
示例4: handle_fk_field
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def handle_fk_field(self, obj, field):
if self.use_natural_foreign_keys and hasattr(field.rel.to, 'natural_key'):
related = getattr(obj, field.name)
if related:
value = related.natural_key()
else:
value = None
else:
value = getattr(obj, field.get_attname())
if not is_protected_type(value):
value = field.value_to_string(obj)
self._current[field.name] = value
示例5: to_representation
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def to_representation(self, obj):
value = self.model_field.value_from_object(obj)
if is_protected_type(value):
return value
return self.model_field.value_to_string(obj)
示例6: field_to_native
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def field_to_native(self, obj, field_name):
# noinspection PyProtectedMember
value = self.model_field._get_val_from_obj(obj)
if is_protected_type(value):
return value
return self.model_field.value_to_string(obj)
示例7: full_dehydrate
# 需要导入模块: from django.utils import encoding [as 别名]
# 或者: from django.utils.encoding import is_protected_type [as 别名]
def full_dehydrate(self, obj, for_list=False):
"""Convert the given object into a dictionary.
:param for_list: True when the object is being converted to belong
in a list.
"""
if for_list:
allowed_fields = self._meta.list_fields
exclude_fields = self._meta.list_exclude
else:
allowed_fields = self._meta.fields
exclude_fields = self._meta.exclude
data = {}
for field in self._meta.object_class._meta.fields:
# Convert the field name to unicode as some are stored in bytes.
field_name = str(field.name)
# Skip fields that are not allowed.
if allowed_fields is not None and field_name not in allowed_fields:
continue
if exclude_fields is not None and field_name in exclude_fields:
continue
# Get the value from the field and set it in data. The value
# will pass through the dehydrate method if present.
field_obj = getattr(obj, field_name)
dehydrate_method = getattr(self, "dehydrate_%s" % field_name, None)
if dehydrate_method is not None:
data[field_name] = dehydrate_method(field_obj)
else:
value = field.value_from_object(obj)
if is_protected_type(value) or isinstance(value, dict):
data[field_name] = value
elif isinstance(field, ArrayField):
data[field_name] = field.to_python(value)
else:
data[field_name] = field.value_to_string(obj)
# Add permissions that can be performed on this object.
data = self._add_permissions(obj, data)
# Return the data after the final dehydrate.
return self.dehydrate(obj, data, for_list=for_list)