Django admin with external models

I am working on a project with one big central django service (I’ll call it the monolith) and a bunch of other microservices (some are django, some are not). We rely heavily on the monolith’s djando admin interface to manage data on those microservices but we could do it better. Here are two options I have been thinking about:

  1. Create some kind of federated/shared login such that I can run django admin on both the monolith and the microservices. Once I log into the monolith I can click a link to go to the microservice and stayed logged in there. Then I can admin the model object on the service that owns them.

  2. Teach django admin on the monolith to talk to the microservices over an API that those services expose. Most of them already expose this data through RESTful interfaces already. I can of course write some basic django templates to consume these REST services, but maybe something like this already exists? If this does not, I am considering writing a library that will help django admin manage models in external services. I am thinking of a general solution that will perhaps consume a swagger/openapi spec and make those external models available to django in some way. Is there any demand for something like this in the community?

  3. Other ideas?

Am I trying to make django admin do more then it should?

1 Like

I would try options 2 or 3. Trying option 1 and keeping all services’ up to date with secure logins that are somehow synced could be painful. Imagine trying to roll out auth ratelimiting - you’d have to repeat the work on every admin!

For option 2 you cannot really use any of the admin features which ultimately all rely on Django querysets and forms, and you can’t use those without a database connection. You’re better off building your own pages and forms that make the appropriate API calls. These can be embedded in the admin if you still like to use it but if you go for “real” microservices the “admin service” won’t have any data in its own DB, at which point you’re better off building your own thing.

You can also use database-level federation to combine the tables from other services. This breaks isolation but can be useful if you’re only using the admin to read data, like a reporting tool.

Check out the book Monolith to Microservices - Monolith to Microservices [Book] . I just finished it and it has great examples of patterns and many many arguments why you don’t want to do microservices, or rather delay using them until absolutely necessary.

Adding to @adamchainz’s excellent answer -

We don’t use microservices but we do have multiple Django instances - we use CAS as a central authentication providing SSO among a group of applications.

Prior to that, we had a couple of applications that used a custom authenticator to authenticate against a foreign database for password validation. (Each instance had its own user table, but the password validation was done using the central user table.)

You could also define multiple databases within your application to point to those foreign data sources. Again, it breaks the concept of isolation between those applications, but with read-only access to gather information, it may not be all that bad.