| 1 |
=============== |
| 2 |
Migration Guide |
| 3 |
=============== |
| 4 |
|
| 5 |
----------------- |
| 6 |
Enabling warnings |
| 7 |
----------------- |
| 8 |
|
| 9 |
To view deprecations, you may need to enable warnings within Python. This |
| 10 |
can be achieved with either the ``-W`` `flag`__, or with ``PYTHONWARNINGS`` |
| 11 |
`environment variable`__. For example, you could run your test suite like so: |
| 12 |
|
| 13 |
.. code-block:: bash |
| 14 |
|
| 15 |
$ python -W once manage.py test |
| 16 |
|
| 17 |
The above would print all warnings once when they first occur. This is useful |
| 18 |
to know what violations exist in your code (or occasionally in third party |
| 19 |
code). However, it only prints the last line of the stack trace. You can use |
| 20 |
the following to raise the full exception instead: |
| 21 |
|
| 22 |
.. code-block:: bash |
| 23 |
|
| 24 |
$ python -W error manage.py test |
| 25 |
|
| 26 |
__ https://docs.python.org/3.6/using/cmdline.html#cmdoption-W |
| 27 |
__ https://docs.python.org/3.6/using/cmdline.html#envvar-PYTHONWARNINGS |
| 28 |
|
| 29 |
---------------- |
| 30 |
Migrating to 2.0 |
| 31 |
---------------- |
| 32 |
|
| 33 |
This release contains several changes that break forwards compatibility. This |
| 34 |
includes removed features, renamed attributes and arguments, and some reworked |
| 35 |
features. Due to the nature of these changes, it is not feasible to release |
| 36 |
a fully forwards-compatible migration release. Please review the below list of |
| 37 |
changes and update your code accordingly. |
| 38 |
|
| 39 |
|
| 40 |
``Filter.lookup_expr`` list form removed (`#851`__) |
| 41 |
--------------------------------------------------- |
| 42 |
__ https://github.com/carltongibson/django-filter/pull/851 |
| 43 |
|
| 44 |
The ``Filter.lookup_expr`` argument no longer accepts ``None`` or a list of |
| 45 |
expressions. Use the :ref:`LookupChoiceFilter <lookup-choice-filter>` instead. |
| 46 |
|
| 47 |
|
| 48 |
FilterSet ``filter_for_reverse_field`` removed (`#915`__) |
| 49 |
--------------------------------------------------------- |
| 50 |
__ https://github.com/carltongibson/django-filter/pull/915 |
| 51 |
|
| 52 |
The ``filter_for_field`` method now generates filters for reverse relationships, |
| 53 |
removing the need for ``filter_for_reverse_field``. As a result, reverse |
| 54 |
relationships now also obey ``Meta.filter_overrides``. |
| 55 |
|
| 56 |
|
| 57 |
View attributes renamed (`#867`__) |
| 58 |
---------------------------------- |
| 59 |
__ https://github.com/carltongibson/django-filter/pull/867 |
| 60 |
|
| 61 |
Several view-related attributes have been renamed to improve consistency with |
| 62 |
other parts of the library. The following classes are affected: |
| 63 |
|
| 64 |
* DRF ``ViewSet.filter_class`` => ``filterset_class`` |
| 65 |
* DRF ``ViewSet.filter_fields`` => ``filterset_fields`` |
| 66 |
* ``DjangoFilterBackend.default_filter_set`` => ``filterset_base`` |
| 67 |
* ``DjangoFilterBackend.get_filter_class()`` => ``get_filterset_class()`` |
| 68 |
* ``FilterMixin.filter_fields`` => ``filterset_fields`` |
| 69 |
|
| 70 |
|
| 71 |
FilterSet ``Meta.together`` option removed (`#791`__) |
| 72 |
----------------------------------------------------- |
| 73 |
__ https://github.com/carltongibson/django-filter/pull/791 |
| 74 |
|
| 75 |
The ``Meta.together`` has been deprecated in favor of userland implementations |
| 76 |
that override the ``clean`` method of the ``Meta.form`` class. An example will |
| 77 |
be provided in a "recipes" section in future docs. |
| 78 |
|
| 79 |
|
| 80 |
FilterSet "strictness" handling moved to view (`#788`__) |
| 81 |
-------------------------------------------------------- |
| 82 |
__ https://github.com/carltongibson/django-filter/pull/788 |
| 83 |
|
| 84 |
Strictness handling has been removed from the ``FilterSet`` and added to the |
| 85 |
view layer. As a result, the ``FILTERS_STRICTNESS`` setting, ``Meta.strict`` |
| 86 |
option, and ``strict`` argument for the ``FilterSet`` initializer have all |
| 87 |
been removed. |
| 88 |
|
| 89 |
To alter strictness behavior, the appropriate view code should be overridden. |
| 90 |
More details will be provided in future docs. |
| 91 |
|
| 92 |
|
| 93 |
``Filter.name`` renamed to ``Filter.field_name`` (`#792`__) |
| 94 |
----------------------------------------------------------- |
| 95 |
__ https://github.com/carltongibson/django-filter/pull/792 |
| 96 |
|
| 97 |
The filter ``name`` has been renamed to ``field_name`` as a way to disambiguate |
| 98 |
the filter's attribute name on its FilterSet class from the ``field_name`` used |
| 99 |
for filtering purposes. |
| 100 |
|
| 101 |
|
| 102 |
``Filter.widget`` and ``Filter.required`` removed (`#734`__) |
| 103 |
------------------------------------------------------------ |
| 104 |
__ https://github.com/carltongibson/django-filter/pull/734 |
| 105 |
|
| 106 |
The filter class no longer directly stores arguments passed to its form field. |
| 107 |
All arguments are located in the filter's ``.extra`` dict. |
| 108 |
|
| 109 |
|
| 110 |
``MultiWidget`` replaced by ``SuffixedMultiWidget`` (`#770`__) |
| 111 |
-------------------------------------------------------------- |
| 112 |
__ https://github.com/carltongibson/django-filter/pull/770 |
| 113 |
|
| 114 |
``RangeWidget``, ``DateRangeWidget``, and ``LookupTypeWidget`` now inherit from |
| 115 |
``SuffixedMultiWidget``, changing the suffixes of their query param names. For |
| 116 |
example, ``RangeWidget`` now has ``_min`` and ``_max`` suffixes instead of |
| 117 |
``_0`` and ``_1``. |
| 118 |
|
| 119 |
|
| 120 |
Filters like ``RangeFilter, DateRangeFilter, DateTimeFromToRangeFilter...`` (`#770`__) |
| 121 |
-------------------------------------------------------------------------------------- |
| 122 |
__ https://github.com/carltongibson/django-filter/pull/770 |
| 123 |
|
| 124 |
As they depend on ``MultiWidget``, they need to be adjusted. In 1.0 release |
| 125 |
parameters were provided using ``_0`` and ``_1`` as suffix``. For example, |
| 126 |
a parameter ``creation_date`` using``DateRangeFilter`` will expect |
| 127 |
``creation_date_after`` and ``creation_date_before`` instead of |
| 128 |
``creation_date_0`` and ``creation_date_1``. |
| 129 |
|
| 130 |
|
| 131 |
---------------- |
| 132 |
Migrating to 1.0 |
| 133 |
---------------- |
| 134 |
|
| 135 |
The 1.0 release of django-filter introduces several API changes and refinements |
| 136 |
that break forwards compatibility. Below is a list of deprecations and |
| 137 |
instructions on how to migrate to the 1.0 release. A forwards-compatible 0.15 |
| 138 |
release has also been created to help with migration. It is compatible with |
| 139 |
both the existing and new APIs and will raise warnings for deprecated behavior. |
| 140 |
|
| 141 |
|
| 142 |
MethodFilter and Filter.action replaced by Filter.method (`#382`__) |
| 143 |
------------------------------------------------------------------- |
| 144 |
__ https://github.com/carltongibson/django-filter/pull/382 |
| 145 |
|
| 146 |
The functionality of ``MethodFilter`` and ``Filter.action`` has been merged |
| 147 |
together and replaced by the ``Filter.method`` parameter. The ``method`` |
| 148 |
parameter takes either a callable or the name of a ``FilterSet`` method. The |
| 149 |
signature now takes an additional ``name`` argument that is the name of the |
| 150 |
model field to be filtered on. |
| 151 |
|
| 152 |
Since ``method`` is now a parameter of all filters, inputs are validated and |
| 153 |
cleaned by its ``field_class``. The function will receive the cleaned value |
| 154 |
instead of the raw value. |
| 155 |
|
| 156 |
.. code-block:: python |
| 157 |
|
| 158 |
# 0.x |
| 159 |
class UserFilter(FilterSet): |
| 160 |
last_login = filters.MethodFilter() |
| 161 |
|
| 162 |
def filter_last_login(self, qs, value): |
| 163 |
# try to convert value to datetime, which may fail. |
| 164 |
if value and looks_like_a_date(value): |
| 165 |
value = datetime(value) |
| 166 |
|
| 167 |
return qs.filter(last_login=value}) |
| 168 |
|
| 169 |
|
| 170 |
# 1.0 |
| 171 |
class UserFilter(FilterSet): |
| 172 |
last_login = filters.CharFilter(method='filter_last_login') |
| 173 |
|
| 174 |
def filter_last_login(self, qs, name, value): |
| 175 |
return qs.filter(**{name: value}) |
| 176 |
|
| 177 |
|
| 178 |
QuerySet methods are no longer proxied (`#440`__) |
| 179 |
------------------------------------------------- |
| 180 |
__ https://github.com/carltongibson/django-filter/pull/440 |
| 181 |
|
| 182 |
The ``__iter__()``, ``__len__()``, ``__getitem__()``, ``count()`` methods are |
| 183 |
no longer proxied from the queryset. To fix this, call the methods on the |
| 184 |
``.qs`` property itself. |
| 185 |
|
| 186 |
.. code-block:: python |
| 187 |
|
| 188 |
f = UserFilter(request.GET, queryset=User.objects.all()) |
| 189 |
|
| 190 |
# 0.x |
| 191 |
for obj in f: |
| 192 |
... |
| 193 |
|
| 194 |
# 1.0 |
| 195 |
for obj in f.qs: |
| 196 |
... |
| 197 |
|
| 198 |
|
| 199 |
Filters no longer autogenerated when Meta.fields is not specified (`#450`__) |
| 200 |
---------------------------------------------------------------------------- |
| 201 |
__ https://github.com/carltongibson/django-filter/pull/450 |
| 202 |
|
| 203 |
FilterSets had an undocumented behavior of autogenerating filters for all |
| 204 |
model fields when either ``Meta.fields`` was not specified or when set to |
| 205 |
``None``. This can lead to potentially unsafe data or schema exposure and |
| 206 |
has been deprecated in favor of explicitly setting ``Meta.fields`` to the |
| 207 |
``'__all__'`` special value. You may also blacklist fields by setting |
| 208 |
the ``Meta.exclude`` attribute. |
| 209 |
|
| 210 |
.. code-block:: python |
| 211 |
|
| 212 |
class UserFilter(FilterSet): |
| 213 |
class Meta: |
| 214 |
model = User |
| 215 |
fields = '__all__' |
| 216 |
|
| 217 |
# or |
| 218 |
class UserFilter(FilterSet): |
| 219 |
class Meta: |
| 220 |
model = User |
| 221 |
exclude = ['password'] |
| 222 |
|
| 223 |
|
| 224 |
Move FilterSet options to Meta class (`#430`__) |
| 225 |
----------------------------------------------- |
| 226 |
__ https://github.com/carltongibson/django-filter/issues/430 |
| 227 |
|
| 228 |
Several ``FilterSet`` options have been moved to the ``Meta`` class to prevent |
| 229 |
potential conflicts with declared filter names. This includes: |
| 230 |
|
| 231 |
* ``filter_overrides`` |
| 232 |
* ``strict`` |
| 233 |
* ``order_by_field`` |
| 234 |
|
| 235 |
.. code-block:: python |
| 236 |
|
| 237 |
# 0.x |
| 238 |
class UserFilter(FilterSet): |
| 239 |
filter_overrides = {} |
| 240 |
strict = STRICTNESS.RAISE_VALIDATION_ERROR |
| 241 |
order_by_field = 'order' |
| 242 |
... |
| 243 |
|
| 244 |
# 1.0 |
| 245 |
class UserFilter(FilterSet): |
| 246 |
... |
| 247 |
|
| 248 |
class Meta: |
| 249 |
filter_overrides = {} |
| 250 |
strict = STRICTNESS.RAISE_VALIDATION_ERROR |
| 251 |
order_by_field = 'order' |
| 252 |
|
| 253 |
|
| 254 |
FilterSet ordering replaced by OrderingFilter (`#472`__) |
| 255 |
-------------------------------------------------------- |
| 256 |
__ https://github.com/carltongibson/django-filter/pull/472 |
| 257 |
|
| 258 |
The FilterSet ordering options and methods have been deprecated and replaced |
| 259 |
by :ref:`OrderingFilter <ordering-filter>`. Deprecated options include: |
| 260 |
|
| 261 |
|
| 262 |
* ``Meta.order_by`` |
| 263 |
* ``Meta.order_by_field`` |
| 264 |
|
| 265 |
These options retain backwards compatibility with the following caveats: |
| 266 |
|
| 267 |
* ``order_by`` asserts that ``Meta.fields`` is not using the dict syntax. This |
| 268 |
previously was undefined behavior, however the migration code is unable to |
| 269 |
support it. |
| 270 |
* Prior, if no ordering was specified in the request, the FilterSet implicitly |
| 271 |
filtered by the first param in the ``order_by`` option. This behavior cannot |
| 272 |
be easily emulated but can be fixed by ensuring that the passed in queryset |
| 273 |
explicitly calls ``.order_by()``. |
| 274 |
|
| 275 |
.. code-block:: python |
| 276 |
|
| 277 |
filterset = MyFilterSet(queryset=MyModel.objects.order_by('field')) |
| 278 |
|
| 279 |
The following methods are deprecated and will raise an assertion if present |
| 280 |
on the FilterSet: |
| 281 |
|
| 282 |
* ``.get_order_by()`` |
| 283 |
* ``.get_ordering_field()`` |
| 284 |
|
| 285 |
To fix this, simply remove the methods from your class. You can subclass |
| 286 |
``OrderingFilter`` to migrate any custom logic. |
| 287 |
|
| 288 |
|
| 289 |
Deprecated ``FILTERS_HELP_TEXT_FILTER`` and ``FILTERS_HELP_TEXT_EXCLUDE`` (`#437`__) |
| 290 |
------------------------------------------------------------------------------------ |
| 291 |
__ https://github.com/carltongibson/django-filter/pull/437 |
| 292 |
|
| 293 |
Generated filter labels in 1.0 will be more descriptive, including humanized |
| 294 |
text about the lookup being performed and if the filter is an exclusion filter. |
| 295 |
|
| 296 |
These settings will no longer have an effect and will be removed in the 1.0 release. |
| 297 |
|
| 298 |
|
| 299 |
DRF filter backend raises ``TemplateDoesNotExist`` exception (`#562`__) |
| 300 |
----------------------------------------------------------------------- |
| 301 |
__ https://github.com/carltongibson/django-filter/issues/562 |
| 302 |
|
| 303 |
Templates are now provided by django-filter. If you are receiving this error, |
| 304 |
you may need to add ``'django_filters'`` to your ``INSTALLED_APPS`` setting. |
| 305 |
Alternatively, you could provide your own templates. |