Skip to content

Commit 82d8dbc

Browse files
Convert MultipleChoiceField to List of type String (#611)
1 parent f33223d commit 82d8dbc

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

docs/filtering.rst

+26
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ create your own ``FilterSet``. You can pass it directly as follows:
123123
class AnimalFilter(django_filters.FilterSet):
124124
# Do case-insensitive lookups on 'name'
125125
name = django_filters.CharFilter(lookup_expr=['iexact'])
126+
# Allow multiple genera to be selected at once
127+
genera = django_filters.MultipleChoiceFilter(
128+
field_name='genus',
129+
choices=(
130+
('Canis', 'Canis'),
131+
('Panthera', 'Panthera'),
132+
('Seahorse', 'Seahorse')
133+
)
134+
)
126135
127136
class Meta:
128137
model = Animal
@@ -135,6 +144,22 @@ create your own ``FilterSet``. You can pass it directly as follows:
135144
all_animals = DjangoFilterConnectionField(AnimalNode,
136145
filterset_class=AnimalFilter)
137146
147+
148+
If you were interested in selecting all dogs and cats, you might query as follows:
149+
150+
.. code::
151+
152+
query {
153+
allAnimals(genera: ["Canis", "Panthera"]) {
154+
edges {
155+
node {
156+
id,
157+
name
158+
}
159+
}
160+
}
161+
}
162+
138163
You can also specify the ``FilterSet`` class using the ``filterset_class``
139164
parameter when defining your ``DjangoObjectType``, however, this can't be used
140165
in unison with the ``filter_fields`` parameter:
@@ -162,6 +187,7 @@ in unison with the ``filter_fields`` parameter:
162187
animal = relay.Node.Field(AnimalNode)
163188
all_animals = DjangoFilterConnectionField(AnimalNode)
164189
190+
165191
The context argument is passed on as the `request argument <http://django-filter.readthedocs.io/en/master/guide/usage.html#request-based-filtering>`__
166192
in a ``django_filters.FilterSet`` instance. You can use this to customize your
167193
filters to be context-dependent. We could modify the ``AnimalFilter`` above to

graphene_django/forms/converter.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ def convert_form_field_to_float(field):
5555
return Float(description=field.help_text, required=field.required)
5656

5757

58+
@convert_form_field.register(forms.MultipleChoiceField)
59+
def convert_form_field_to_string_list(field):
60+
return List(String, description=field.help_text, required=field.required)
61+
62+
5863
@convert_form_field.register(forms.ModelMultipleChoiceField)
5964
@convert_form_field.register(GlobalIDMultipleChoiceField)
60-
def convert_form_field_to_list(field):
65+
def convert_form_field_to_id_list(field):
6166
return List(ID, required=field.required)
6267

6368

graphene_django/forms/tests/test_converter.py

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def test_should_choice_convert_string():
6666
assert_conversion(forms.ChoiceField, String)
6767

6868

69+
def test_should_multiple_choice_convert_list():
70+
assert_conversion(forms.MultipleChoiceField, List)
71+
72+
6973
def test_should_base_field_convert_string():
7074
assert_conversion(forms.Field, String)
7175

0 commit comments

Comments
 (0)