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


Python serializers.ChoiceField方法代碼示例

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


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

示例1: __init__

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import ChoiceField [as 別名]
def __init__(self, *args, **kwargs):
        super(YaraRuleSerializer, self).__init__(*args, **kwargs)

        try:
            source_blank = not self.retrieve_request_group().groupmeta.source_required
            self.fields['source'] = serializers.ChoiceField(choices=self.retrieve_sources(),
                                                            allow_blank=source_blank)

            category_blank = not self.retrieve_request_group().groupmeta.category_required
            self.fields['category'] = serializers.ChoiceField(choices=self.retrieve_categories(),
                                                              allow_blank=category_blank)

            self.fields['rule_content'].required = True

        except AttributeError:
            pass 
開發者ID:PUNCH-Cyber,項目名稱:YaraGuardian,代碼行數:18,代碼來源:REST_serializers.py

示例2: __init__

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import ChoiceField [as 別名]
def __init__(self, *args, **kwargs):
        """
        Add the residence ChoiceField on the serializer based on the existence
        of Residence models
        :param args: arguments
        :param kwargs: keyword arguments
        """

        super().__init__(*args, **kwargs)

        try:
            residences = Residence.objects.all()
        except ProgrammingError:
            residences = list()

        self.fields['residence'] = serializers.ChoiceField(
            choices=[
                (residence.id, residence.name)
                for residence in residences
            ],
            write_only=True,
        ) 
開發者ID:IMGIITRoorkee,項目名稱:omniport-backend,代碼行數:24,代碼來源:residential_information.py

示例3: __init__

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import ChoiceField [as 別名]
def __init__(self, *args, **kwargs):
        """Initialize the OrderSerializer."""
        super().__init__(*args, **kwargs)

        if self.tag_keys is not None:
            fkwargs = {"choices": OrderSerializer.ORDER_CHOICES, "required": False}
            self._init_tag_keys(serializers.ChoiceField, fkwargs=fkwargs) 
開發者ID:project-koku,項目名稱:koku,代碼行數:9,代碼來源:serializers.py

示例4: convert_serializer_field

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import ChoiceField [as 別名]
def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True):
    """
    Converts a django rest frameworks field to a graphql field
    and marks the field as required if we are creating an input type
    and the field itself is required
    """

    if isinstance(field, serializers.ChoiceField) and not convert_choices_to_enum:
        graphql_type = graphene.String
    else:
        graphql_type = get_graphene_type_from_serializer_field(field)

    args = []
    kwargs = {"description": field.help_text, "required": is_input and field.required}

    # if it is a tuple or a list it means that we are returning
    # the graphql type and the child type
    if isinstance(graphql_type, (list, tuple)):
        kwargs["of_type"] = graphql_type[1]
        graphql_type = graphql_type[0]

    if isinstance(field, serializers.ModelSerializer):
        if is_input:
            graphql_type = convert_serializer_to_input_type(field.__class__)
        else:
            global_registry = get_global_registry()
            field_model = field.Meta.model
            args = [global_registry.get_type_for_model(field_model)]
    elif isinstance(field, serializers.ListSerializer):
        field = field.child
        if is_input:
            kwargs["of_type"] = convert_serializer_to_input_type(field.__class__)
        else:
            del kwargs["of_type"]
            global_registry = get_global_registry()
            field_model = field.Meta.model
            args = [global_registry.get_type_for_model(field_model)]

    return graphql_type(*args, **kwargs) 
開發者ID:graphql-python,項目名稱:graphene-django,代碼行數:41,代碼來源:serializer_converter.py

示例5: test_should_choice_convert_enum

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import ChoiceField [as 別名]
def test_should_choice_convert_enum():
    field = assert_conversion(
        serializers.ChoiceField,
        graphene.Enum,
        choices=[("h", "Hello"), ("w", "World")],
        source="word",
    )
    assert field._meta.enum.__members__["H"].value == "h"
    assert field._meta.enum.__members__["H"].description == "Hello"
    assert field._meta.enum.__members__["W"].value == "w"
    assert field._meta.enum.__members__["W"].description == "World" 
開發者ID:graphql-python,項目名稱:graphene-django,代碼行數:13,代碼來源:test_field_converter.py

示例6: test_should_choice_convert_string_if_enum_disabled

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import ChoiceField [as 別名]
def test_should_choice_convert_string_if_enum_disabled():
    assert_conversion(
        serializers.ChoiceField,
        graphene.String,
        choices=[("h", "Hello"), ("w", "World")],
        source="word",
        convert_choices_to_enum=False,
    ) 
開發者ID:graphql-python,項目名稱:graphene-django,代碼行數:10,代碼來源:test_field_converter.py


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