Add a custom API metadata class to include filters on OPTIONS responses.

This commit is contained in:
Christopher Charbonneau Wells 2017-12-05 19:27:28 -05:00
parent 26a3c1c721
commit 9e220d29f4
2 changed files with 19 additions and 2 deletions

16
api/metadata.py Normal file
View File

@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from rest_framework import metadata
class APIMetadata(metadata.SimpleMetadata):
"""
Custom metadata class for OPTIONS responses.
"""
def determine_metadata(self, request, view):
data = super(APIMetadata, self).determine_metadata(request, view)
data.pop('description')
if hasattr(view, 'filter_fields'):
data.update({'filters': view.filter_fields})
return data

View File

@ -152,14 +152,15 @@ REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': [ 'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend', 'django_filters.rest_framework.DjangoFilterBackend',
], ],
'DEFAULT_METADATA_CLASS': 'api.metadata.APIMetadata',
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.LimitOffsetPagination',
'DEFAULT_PERMISSION_CLASSES': [ 'DEFAULT_PERMISSION_CLASSES': [
'api.permissions.BabyBuddyDjangoModelPermissions' 'api.permissions.BabyBuddyDjangoModelPermissions'
], ],
'DEFAULT_RENDERER_CLASSES': [ 'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.JSONRenderer',
], ],
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100 'PAGE_SIZE': 100
} }