Using a different user model for the admin

I’m working on an application that needs to integrate a legacy database. The legacy database comes with its own user table, which is very different from the Django default user, and it cannot be modified to add the missing columns or logic.

The application uses OAuth2 for authenticating users, and I need to use this user model when it comes to the “main” application usage (i.e., it needs to be the user model that’s used for authenticating requests).

However, for a whole bunch of reasons, using this model for the admin is proving pretty painful and it’s just not what I’m looking for.

Is there a way to keep this user model as the one used to populate sessions, request.user etc., but use the default Django user model for the admin application?

Are you using the passwords stored in that model for authentication? Or are you using information in that model for authorization? (Or both?)

What we did in a similar situation was to create our own authentication back-end that would authenticate against the legacy model. I would set it up such that the ModelBackend authenticates first, then the legacy system second. This should bypass the issues with the admin.

The legacy model is to be used with OAuth2 authentication. When the OAuth2 auth backend receives the token and uses it to fetch user data, we use an ID returned in the payload by the auth server to associate an instance of this legacy user model. I’m using drf-oauth2 for this, which then creates a token used in subsequent requests, and should associate it with the legacy user model instance.

All I need is to be able to populate request.user with the user associated to the token.

I’m not sure if this answers your question though, maybe I misunderstood.

That’s one of the functions and purposes of the authentication backend. If the backend you are using is not doing that for you, then it needs to be modified for that purpose.

See Customizing authentication in Django | Django documentation | Django for more details.