From b4291e9e226dd11aea0a6181fab5713838440085 Mon Sep 17 00:00:00 2001 From: Kimbrian Canavan Date: Wed, 3 Apr 2019 13:50:10 +1100 Subject: [PATCH 1/3] Convert MultipleChoiceField to List of type String --- graphene_django/forms/converter.py | 7 ++++++- graphene_django/forms/tests/test_converter.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/graphene_django/forms/converter.py b/graphene_django/forms/converter.py index 7b154b4ca..077e9842e 100644 --- a/graphene_django/forms/converter.py +++ b/graphene_django/forms/converter.py @@ -55,9 +55,14 @@ def convert_form_field_to_float(field): 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(GlobalIDMultipleChoiceField) -def convert_form_field_to_list(field): +def convert_form_field_to_id_list(field): return List(ID, required=field.required) diff --git a/graphene_django/forms/tests/test_converter.py b/graphene_django/forms/tests/test_converter.py index 955b95290..29a441913 100644 --- a/graphene_django/forms/tests/test_converter.py +++ b/graphene_django/forms/tests/test_converter.py @@ -66,6 +66,10 @@ def test_should_choice_convert_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(): assert_conversion(forms.Field, String) From 95868465ef7ea602c6bb9014c7085c9853a6389d Mon Sep 17 00:00:00 2001 From: Kimbrian Canavan Date: Wed, 17 Apr 2019 11:43:55 +1000 Subject: [PATCH 2/3] Add multiple choice filtering example to docs --- docs/filtering.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/filtering.rst b/docs/filtering.rst index cab61ecbf..584c0a4d5 100644 --- a/docs/filtering.rst +++ b/docs/filtering.rst @@ -123,6 +123,11 @@ create your own ``FilterSet``. You can pass it directly as follows: class AnimalFilter(django_filters.FilterSet): # Do case-insensitive lookups on 'name' 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: model = Animal @@ -135,6 +140,22 @@ create your own ``FilterSet``. You can pass it directly as follows: all_animals = DjangoFilterConnectionField(AnimalNode, 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`` parameter when defining your ``DjangoObjectType``, however, this can't be used in unison with the ``filter_fields`` parameter: @@ -162,6 +183,7 @@ in unison with the ``filter_fields`` parameter: animal = relay.Node.Field(AnimalNode) all_animals = DjangoFilterConnectionField(AnimalNode) + The context argument is passed on as the `request argument `__ 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 From 903cebbe0c081f86ebab542d772c8771d5bebcc3 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Sat, 25 Apr 2020 14:12:40 +0100 Subject: [PATCH 3/3] Re format code example --- docs/filtering.rst | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/filtering.rst b/docs/filtering.rst index 584c0a4d5..d22d349f9 100644 --- a/docs/filtering.rst +++ b/docs/filtering.rst @@ -124,10 +124,14 @@ create your own ``FilterSet``. You can pass it directly as follows: # Do case-insensitive lookups on 'name' 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'))) + genera = django_filters.MultipleChoiceFilter( + field_name='genus', + choices=( + ('Canis', 'Canis'), + ('Panthera', 'Panthera'), + ('Seahorse', 'Seahorse') + ) + ) class Meta: model = Animal