Skip to content

Commit 9586846

Browse files
kimbriancanavanjkimbo
authored andcommitted
Add multiple choice filtering example to docs
1 parent b4291e9 commit 9586846

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/filtering.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ 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(field_name='genus',
128+
choices=(('Canis', 'Canis'),
129+
('Panthera', 'Panthera'),
130+
('Seahorse', 'Seahorse')))
126131
127132
class Meta:
128133
model = Animal
@@ -135,6 +140,22 @@ create your own ``FilterSet``. You can pass it directly as follows:
135140
all_animals = DjangoFilterConnectionField(AnimalNode,
136141
filterset_class=AnimalFilter)
137142
143+
144+
If you were interested in selecting all dogs and cats, you might query as follows:
145+
146+
.. code::
147+
148+
query {
149+
allAnimals(genera: ["Canis", "Panthera"]) {
150+
edges {
151+
node {
152+
id,
153+
name
154+
}
155+
}
156+
}
157+
}
158+
138159
You can also specify the ``FilterSet`` class using the ``filterset_class``
139160
parameter when defining your ``DjangoObjectType``, however, this can't be used
140161
in unison with the ``filter_fields`` parameter:
@@ -162,6 +183,7 @@ in unison with the ``filter_fields`` parameter:
162183
animal = relay.Node.Field(AnimalNode)
163184
all_animals = DjangoFilterConnectionField(AnimalNode)
164185
186+
165187
The context argument is passed on as the `request argument <http://django-filter.readthedocs.io/en/master/guide/usage.html#request-based-filtering>`__
166188
in a ``django_filters.FilterSet`` instance. You can use this to customize your
167189
filters to be context-dependent. We could modify the ``AnimalFilter`` above to

0 commit comments

Comments
 (0)