當前位置: 首頁>>代碼示例>>Python>>正文


Python OrderedDict.get方法代碼示例

本文整理匯總了Python中rest_framework.compat.OrderedDict.get方法的典型用法代碼示例。如果您正苦於以下問題:Python OrderedDict.get方法的具體用法?Python OrderedDict.get怎麽用?Python OrderedDict.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rest_framework.compat.OrderedDict的用法示例。


在下文中一共展示了OrderedDict.get方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_field_info

# 需要導入模塊: from rest_framework.compat import OrderedDict [as 別名]
# 或者: from rest_framework.compat.OrderedDict import get [as 別名]
    def get_field_info(self, field):
        """
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        """
        field_info = OrderedDict()
        field_info['type'] = self.label_lookup[field]
        field_info['required'] = getattr(field, 'required', False)

        attrs = [
            'read_only', 'label', 'help_text',
            'min_length', 'max_length',
            'min_value', 'max_value'
        ]

        for attr in attrs:
            value = getattr(field, attr, None)
            if value is not None and value != '':
                field_info[attr] = force_text(value, strings_only=True)

        if not field_info.get('read_only') and hasattr(field, 'choices'):
            field_info['choices'] = [
                {
                    'value': choice_value,
                    'display_name': force_text(choice_name, strings_only=True)
                }
                for choice_value, choice_name in field.choices.items()
            ]

        return field_info
開發者ID:AndrewIngram,項目名稱:django-rest-framework,代碼行數:32,代碼來源:metadata.py

示例2: get_field_info

# 需要導入模塊: from rest_framework.compat import OrderedDict [as 別名]
# 或者: from rest_framework.compat.OrderedDict import get [as 別名]
    def get_field_info(self, field):
        field_info = super(LinkMetaData, self).get_field_info(field)
        field_info = OrderedDict()
        field_info['type'] = self.label_lookup[field]
        field_info['required'] = getattr(field, 'required', False)

        attrs = [
            'read_only', 'label', 'help_text',
            'min_length', 'max_length',
            'min_value', 'max_value'
        ]

        for attr in attrs:
            value = getattr(field, attr, None)
            if value is not None and value != '':
                field_info[attr] = force_text(value, strings_only=True)

        if getattr(field, 'child', None):
            field_info['child'] = self.get_field_info(field.child)
        elif getattr(field, 'fields', None):
            field_info['children'] = self.get_serializer_info(field)
        if not field_info.get('read_only') and hasattr(field, 'choices'):
            field_info['choices'] = []
            for data, junk in field.choices.items():
                if data and (data != 'None'):
                    data = ast.literal_eval(data)
                    field_info['choices'].append(
                        {
                            'value': data['id'],
                            'display_name': force_text(
                                data['name'],
                                strings_only=True)
                        }
                    )
        return field_info
開發者ID:ozmax,項目名稱:aggro_links,代碼行數:37,代碼來源:metadata.py

示例3: get_field_info

# 需要導入模塊: from rest_framework.compat import OrderedDict [as 別名]
# 或者: from rest_framework.compat.OrderedDict import get [as 別名]
    def get_field_info(self, field):
        """
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        """
        field_info = OrderedDict()
        field_info["type"] = self.label_lookup[field]
        field_info["required"] = getattr(field, "required", False)

        attrs = ["read_only", "label", "help_text", "min_length", "max_length", "min_value", "max_value"]

        for attr in attrs:
            value = getattr(field, attr, None)
            if value is not None and value != "":
                field_info[attr] = force_text(value, strings_only=True)

        if not field_info.get("read_only") and hasattr(field, "choices"):
            field_info["choices"] = [
                {"value": choice_value, "display_name": force_text(choice_name, strings_only=True)}
                for choice_value, choice_name in field.choices.items()
            ]

        return field_info
開發者ID:smitp,項目名稱:django-rest-framework,代碼行數:25,代碼來源:metadata.py

示例4: JsonApiAdapter

# 需要導入模塊: from rest_framework.compat import OrderedDict [as 別名]
# 或者: from rest_framework.compat.OrderedDict import get [as 別名]
class JsonApiAdapter(object):
    def __init__(self, renderer, serialized_data, serializer=None):
        self.renderer = renderer
        self.serialized_data = serialized_data
        if serializer:
            self.serializer = get_serializer(serializer)
        else:
            self.serializer = get_serializer(serialized_data.serializer)
        self.hash = OrderedDict({"data": []})
        self.included_set = set()

    def serializable_hash(self):
        if isinstance(self.serialized_data, list):
            for obj in self.serialized_data:
                result = JsonApiAdapter(self.renderer, obj,
                                        self.serializer).serializable_hash()
                self.hash["data"].append(result.get("data"))
                if result.get("included"):
                    if "included" not in self.hash:
                        self.hash["included"] = []
                    for result in result.get("included"):
                        set_key = "-".join([result.get("type"),
                                            result.get("id")])
                        if set_key not in self.included_set:
                            self.hash["included"].append(result)
                            self.included_set.add(set_key)
        else:
            self.hash["data"] = self.attributes_for_serialized_data(
                self.serialized_data, self.serializer)
            self.add_resource_relationships(
                self.hash["data"], self.serialized_data, self.serializer)
        return self.hash

    def add_relationships(self, resource, rel_name, relationship):
        dash_name = dasherize(rel_name)
        if dash_name not in resource["relationships"]:
            resource["relationships"][dash_name] = OrderedDict({
                "data": []
            })
        if relationship.get("data"):
            for data in relationship.get("data"):
                try:
                    rel_id = data.get("id")  # Serialized data
                except AttributeError:
                    rel_id = data  # Only IDs
                resource["relationships"][dash_name]["data"].append(
                    OrderedDict([
                        ("id", force_text(rel_id)),
                        ("type", relationship.get("type")),
                    ])
                )

    def add_relationship(self, resource, rel_name, relationship):
        dash_name = dasherize(rel_name)
        if dash_name not in resource["relationships"]:
            resource["relationships"][dash_name] = OrderedDict({
                "data": None
            })
        if relationship.get("data"):
            try:
                rel_id = relationship.get("data").get("id")  # Serialized data
            except AttributeError:
                rel_id = relationship.get("data")  # Only ID
            resource["relationships"][dasherize(rel_name)]["data"] = \
                OrderedDict([
                    ("id", force_text(rel_id)),
                    ("type", relationship.get("type")),
                ])

    def add_included(self, rel_name, relationship, parent=None):
        included_serializer = self.get_included_serializer(
            relationship.get("parent_serializer"), rel_name)
        if not included_serializer:
            return
        serialized_data = relationship.get("data")
        if not isinstance(serialized_data, list):
            serialized_data = [serialized_data]
        included_data = []
        for item in serialized_data:
            if isinstance(item, six.integer_types):
                # Only ID
                data = self.get_included_data(
                    rel_name, item, included_serializer)
                if data:
                    included_data.append(data)

        resource_path = ".".join([parent, rel_name] if parent else [rel_name])
        if self.include_assoc(resource_path):
            if "included" not in self.hash:
                self.hash["included"] = []
            for data in included_data:
                attrs = self.attributes_for_serialized_data(
                    data, included_serializer)
                self.add_resource_relationships(
                    attrs, data, included_serializer, add_included=False)
                if attrs not in self.hash.get("included"):
                    self.hash["included"].append(attrs)
        if self.include_nested_assoc(resource_path):
            for data in included_data:
                relationships = self.get_relationships_data(
#.........這裏部分代碼省略.........
開發者ID:pombredanne,項目名稱:django-rest-framework-jsonapi,代碼行數:103,代碼來源:renderers.py


注:本文中的rest_framework.compat.OrderedDict.get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。