I am using Django REST Framework (DRF) for my API. In particular, I have set up several routes using the ModelViewSet
, without much additional coding. In particular, I have a route like
/api/v1/collection/<key>
From all the possible keys, I do need to block one specific key, say special
. Any request with any HTTP verb to /api/v1/collection/special
should result in a HTTP 404 error.
I can think of two approaches:
- Override some method on the
ViewSet
hierarchy. Which one? - In
urls.py
, set up a higher-precedence URL route that intercepts this URL, likepath("collection/special", view=<404View>, name="collection-exception")
. Does this make sense? What would be the appropriate exception view to route to?
What is the recommended approach? Any of the above, or something else?