Skip to content

Commit 19afd2c

Browse files
author
Ryan P Kilby
committed
Document multi-field approach to handle null value
1 parent 2cdbf6f commit 19afd2c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

docs/guide/tips.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ the ``null_label`` parameter. More details in the ``ChoiceFilter`` reference
142142
queryset=Category.objects.all(),
143143
)
144144

145+
Solution 3: Comining fields w/ ``MultiValueField``
146+
""""""""""""""""""""""""""""""""""""""""""""""""""
147+
148+
An alternative approach is to use Django's ``MultiValueField`` to manually add
149+
in a ``BooleanField`` to handle null values. Proof of concept:
150+
https://github.com/carltongibson/django-filter/issues/446
151+
145152

146153
Filtering by an empty string
147154
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -153,7 +160,7 @@ interpreted as a skipped filter.
153160

154161
GET http://localhost/api/my-model?myfield=
155162

156-
Solution 1: magic values
163+
Solution 1: Magic values
157164
""""""""""""""""""""""""
158165

159166
You can override the ``filter()`` method of a filter class to specifically check
@@ -176,7 +183,7 @@ for magic values. This is similar to the ``ChoiceFilter``'s null value handling.
176183
return qs.distinct() if self.distinct else qs
177184

178185

179-
Solution 2: empty string filter
186+
Solution 2: Empty string filter
180187
"""""""""""""""""""""""""""""""
181188

182189
It would also be possible to create an empty value filter that exhibits the same

0 commit comments

Comments
 (0)