mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-25 02:54:06 +03:00
Convert MultipleChoiceField to List of type String (#611)
This commit is contained in:
parent
f33223daa7
commit
82d8dbc893
|
@ -123,6 +123,15 @@ create your own ``FilterSet``. You can pass it directly as follows:
|
||||||
class AnimalFilter(django_filters.FilterSet):
|
class AnimalFilter(django_filters.FilterSet):
|
||||||
# Do case-insensitive lookups on 'name'
|
# Do case-insensitive lookups on 'name'
|
||||||
name = django_filters.CharFilter(lookup_expr=['iexact'])
|
name = django_filters.CharFilter(lookup_expr=['iexact'])
|
||||||
|
# Allow multiple genera to be selected at once
|
||||||
|
genera = django_filters.MultipleChoiceFilter(
|
||||||
|
field_name='genus',
|
||||||
|
choices=(
|
||||||
|
('Canis', 'Canis'),
|
||||||
|
('Panthera', 'Panthera'),
|
||||||
|
('Seahorse', 'Seahorse')
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Animal
|
model = Animal
|
||||||
|
@ -135,6 +144,22 @@ create your own ``FilterSet``. You can pass it directly as follows:
|
||||||
all_animals = DjangoFilterConnectionField(AnimalNode,
|
all_animals = DjangoFilterConnectionField(AnimalNode,
|
||||||
filterset_class=AnimalFilter)
|
filterset_class=AnimalFilter)
|
||||||
|
|
||||||
|
|
||||||
|
If you were interested in selecting all dogs and cats, you might query as follows:
|
||||||
|
|
||||||
|
.. code::
|
||||||
|
|
||||||
|
query {
|
||||||
|
allAnimals(genera: ["Canis", "Panthera"]) {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id,
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
You can also specify the ``FilterSet`` class using the ``filterset_class``
|
You can also specify the ``FilterSet`` class using the ``filterset_class``
|
||||||
parameter when defining your ``DjangoObjectType``, however, this can't be used
|
parameter when defining your ``DjangoObjectType``, however, this can't be used
|
||||||
in unison with the ``filter_fields`` parameter:
|
in unison with the ``filter_fields`` parameter:
|
||||||
|
@ -162,6 +187,7 @@ in unison with the ``filter_fields`` parameter:
|
||||||
animal = relay.Node.Field(AnimalNode)
|
animal = relay.Node.Field(AnimalNode)
|
||||||
all_animals = DjangoFilterConnectionField(AnimalNode)
|
all_animals = DjangoFilterConnectionField(AnimalNode)
|
||||||
|
|
||||||
|
|
||||||
The context argument is passed on as the `request argument <http://django-filter.readthedocs.io/en/master/guide/usage.html#request-based-filtering>`__
|
The context argument is passed on as the `request argument <http://django-filter.readthedocs.io/en/master/guide/usage.html#request-based-filtering>`__
|
||||||
in a ``django_filters.FilterSet`` instance. You can use this to customize your
|
in a ``django_filters.FilterSet`` instance. You can use this to customize your
|
||||||
filters to be context-dependent. We could modify the ``AnimalFilter`` above to
|
filters to be context-dependent. We could modify the ``AnimalFilter`` above to
|
||||||
|
|
|
@ -55,9 +55,14 @@ def convert_form_field_to_float(field):
|
||||||
return Float(description=field.help_text, required=field.required)
|
return Float(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
|
@convert_form_field.register(forms.MultipleChoiceField)
|
||||||
|
def convert_form_field_to_string_list(field):
|
||||||
|
return List(String, description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
||||||
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
||||||
def convert_form_field_to_list(field):
|
def convert_form_field_to_id_list(field):
|
||||||
return List(ID, required=field.required)
|
return List(ID, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,10 @@ def test_should_choice_convert_string():
|
||||||
assert_conversion(forms.ChoiceField, String)
|
assert_conversion(forms.ChoiceField, String)
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_multiple_choice_convert_list():
|
||||||
|
assert_conversion(forms.MultipleChoiceField, List)
|
||||||
|
|
||||||
|
|
||||||
def test_should_base_field_convert_string():
|
def test_should_base_field_convert_string():
|
||||||
assert_conversion(forms.Field, String)
|
assert_conversion(forms.Field, String)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user