本文整理汇总了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
示例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,
)
示例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)
示例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)
示例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"
示例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,
)