0

Current situation

  • I have 3 apps in my Django project: auth, polls and school.
  • AUTH_USER_MODEL in my settings is auth.models.User, and it is shared between all apps.
  • the login/ URL in urls.py is also shared between the 3 apps.

What I need to do

I want to add some methods only to the users of the school app. To do this, I created a school.models.User model with proxy = True meta. This model inherits from auth.models.User.

Problem

When a user logs in, request.user contains a "base" auth.models.User object, independently of which app the user is using.

How can I populate request.user with a different user model, depending on the app?

I tried using a custom Authentication Backend as suggested in Django - User proxy model from request, but I have no way to detect which app the user is using, so I can't seem to discriminate between polls and school in the authentication backend.

Kurt Bourbaki
  • 11,984
  • 6
  • 35
  • 53
  • It's quite unusual what you're trying to do but what I would try to do is to resolve current URL to check app name and then based on app name return relevant model object. To do that, you would need an access to request object - thus you would need to write your own authentication middleware. – marxin Apr 24 '18 at 15:59
  • @marxin thank you :) Why is it unusual? What would be the usual way to do something similar? I think I might be doing something wrong with the structure of the project, but I don't know what the alternative could be. – Kurt Bourbaki Apr 25 '18 at 08:21

0 Answers0