I am writing my first REST API (with django-rest-framework).
I am adding URL parameters to filter the results. I understand that the documentation for these parameters belongs to the verb OPTIONS. My code is:
class SuburbViewSet(viewsets.ReadOnlyModelViewSet): """ Retrieves the suburbs (20 per page). GET and OPTIONS allowed. """ model = Suburb serializer_class = SuburbSerializer def get_queryset(self): """ Can filter by region_id, ... - using query parameters in the URL. """ queryset = Suburb.objects.all() region_id = self.request.QUERY_PARAMS.get('region_id', None) if region_id is not None: queryset = queryset.filter(region_id=region_id) return queryset def metadata(self, request): ret = super(SuburbViewSet, self).metadata(request) ret['parameters'] = { "page": { "type": "integer", "description": "The page number", "required": False }, "region_id": { "type": "integer", "description": "The region ID to filter the results", "required": False } } return ret
Is this the best / only REST way (explaining that the parameters are in OPTION)?
As for the django-rest-framework, I have expanded the metadata (self, request) that feel hacked. Did I miss some kind of built-in way to set parameter descriptions?
Thanks!
source share