Filters
Filters are used to define the output of your responses. The only fields returned in the response are the ones specified in the filters. If not specified, the field will not be part of the response.
Note
Important: If filters are not defined, the response will be an empty dictionary.
Filtering Options
- ALWAYS
The response field will always be returned.
- NEVER
The response field will never be returned.
- IF_TRUTHY
Only return if the response field value is a python truthy value.
Structure for defining a filter
FilterName = {
Model: {
'response_field': filtering_option
}
}
Example:
Creating a new filters.py file and defining a filter for a Todo app’s response.
TodoResponseFilter = {
Todo: {
'task': filtering.ALWAYS,
'priority': filtering.ALWAYS,
'created_date': filtering.NEVER,
'completion_status': filtering.IF_TRUTHY
},
}
taskandpriorityfields are set to ALWAYS . Therefore, they will always be returned in the response.create_dateis set to NEVER , thus, it will not appear in the response object.completion_statuswill only be included in the response if it is a python truthy value.
Define Filters
Filters can be defined in four places:
Note
Important: Items lower in the list take precedence over items at the top of the list. For instance, response_filter overrides the default filter defined in settings.py.
- settings.py
Required | Set default filters for the entire application in settings.py by setting
DECLARATIVE_ENDPOINT_DEFAULT_FILTERSto yourfilters.pyfile.Example: Set the Todo filter as the default filter in
settings.py.DECLARATIVE_ENDPOINT_DEFAULT_FILTERS = "todo.filters.TodoResponseFilter"
response_filterOptional | Defines filters for a class and can be used interchangeably with
@endpoint_resource(). To implement, setresponse_filteras a class-level field on yourEndpointDefinition.Default Value |
NoneExample:
class TodoCreationEndpoint: response_filter = filters.TodoResponseFilter
@endpoint_resourceOptional | Defines filters for a class and can be used interchangeably with
response_filter. To implement, setfilter=<filter>in an argument to the@endpoint_resourcedecorator.Default Value |
NoneExample
from django-declarative-apis.machinery import endpoint_resource class TodoDefinition(TodoResourceMixin, machinery.ResourceEndpointDefinition): resource_model = Todo @endpoint_resource(type=Todo, filter=filters.TodoResponseFilter) def resource(self): return Todo.objects.all()
_api_filterOptional | Defines filters for a return object. To implement, set
_api_filteron the object returned from@endpoint_resource.Default Value |
NoneExample
class TodoDefinition(TodoResourceMixin, machinery.ResourceUpdateEndpointDefinition): task = field(required=True, type=str) priority = field(required=True, type=str) completion_status = field(type=bool, default=False) @endpoint_resource(type=Todo) def resource(self): task, created = Todo.objects.get_or_create( task=self.task, priority=self.priority, completion_status=self.completion_status, ) task._api_filter = filters.TodoResponseFilter return task