Adding custom Filtering
Note
It is advised to go through point 2 of Marking models to create REST APIs and Adding Custom Serializer and have an understanding of passing custom view attributes.
For filtering, following keys can be used in the dictionary that would be returned from the getParams()
method of a ViewParams
class:
- to_rest.constants.FILTER_BACKENDS: To specify filter backends
- to_rest.constants.FILTERSET_FIELDS: To specify filterset fields
- to_rest.constants.SEARCH_FIELDS: To specify search fields
- to_rest.constants.ORDERING_FIELDS: To specify ordering fields
- to_rest.constants.ORDERING: To specify default ordering
- to_rest.constants.FILTERSET_CLASS: To specify filterset class
Note
If both to_rest.constants.FILTERSET_CLASS
and to_rest.constants.FILTERSET_FIELDS
are used then to_rest.constants.FILTERSET_CLASS
is given preference and to_rest.constants.FILTERSET_FIELDS
is ignored.
For example, let us consider the below model:
models.py | |
---|---|
For above model, various filtering attributes can be provided via a ViewParams
class. For example:
- Here, test_basics is the directory of app
Ensure that the name of the class is passed to the decorator restifyModel()
in models.py
. For example: