当前位置: 首页>>代码示例>>Python>>正文


Python encoding.is_protected_type方法代码示例

本文整理汇总了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()) 
开发者ID:wagtail,项目名称:django-modelcluster,代码行数:21,代码来源:models.py

示例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) 
开发者ID:erigones,项目名称:esdc-ce,代码行数:21,代码来源:fields.py

示例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) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:11,代码来源:python.py

示例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 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:14,代码来源:python.py

示例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) 
开发者ID:BeanWei,项目名称:Dailyfresh-B2C,代码行数:7,代码来源:fields.py

示例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) 
开发者ID:erigones,项目名称:esdc-ce,代码行数:8,代码来源:fields.py

示例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) 
开发者ID:maas,项目名称:maas,代码行数:46,代码来源:base.py


注:本文中的django.utils.encoding.is_protected_type方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。