Reputation: 23
I am building an API-first app with the Django REST framework, so it has no html views at all and uses only token-based authentication. At the same time, I would like to make use of the Django admin interface, which is no problem, but I am worried about the performance costs as it depends on a lot of apps (sessions, standard auth, messages, csrf, etc.), that are not needed in the main app, but will run on every request.
Is there a way to set admin-specific middlewares to run only on the admin interface?
I know I can subclass them and raise MiddlewareNotUsed on all requests except ones going to the admin site, but maybe there is some build-in or well-known solution to this?
Upvotes: 1
Views: 1697
Reputation: 896
To answer your question, no, I don't think there are built-in solutions that cater to this. But that's probably because your purpose is not a good fit for Django's design and philosophy.
I'm with the comment by Sahil on this. I used to be paranoid about performance too, but I realized that I was underestimating Django's speed (even with all these basic middleware), and that if performance was that critical, I probably shouldn't be using Django in the first place. I'm guessing that disabling these middleware will save an app's response time only unnoticeable milliseconds at most; inevitable network fluctuations might even be more significant. A developer's time is more expensive than the additional hardware that could be thrown at any performance and/or scalability problems that come up.
But if you still want to save on the middleware processing, I have an alternative idea: the convenience functionality provided by the Django admin (that is, CRUD operations) could be replicated quite rapidly with DRF viewsets. Perhaps you could build a corresponding API client using some rapid-development frontend framework for your users. (I was going to say, just use the DRF browsable API, but I remembered that also relies on pretty much the same basic middleware as the Django admin.)
Upvotes: 1