On Jun 18, 2017 10:11 PM, "Victor Hooi" <victorh...@gmail.com> wrote:

Hi,

Say you have multiple custom users, each inheriting from AbstractUser. The
docs mention setting AUTH_USER_MODEL:

https://docs.djangoproject.com/en/1.11/topics/auth/customizi
ng/#substituting-a-custom-user-model

However, what happens if you have *multiple* custom users - which would you
set it to?


The answer is to rethink your user model(s). The idea being that a 'user'
should be thought about at a very high level.

There should only be one canon user model in use as far as system
authentication is concerned. The 'type' of user rarely warrants the use of
a separate model, rather the type would typically be made available as an
attribute of your system User (notice the capital U), restricted by either
a static list of possible choices (https://docs.djangoproject.
com/en/1.11/ref/models/fields/#choices) or by utilizing a foreign key to
another model containing the list of potential user types.

Your User model (which can be named anything you like, I'm just referencing
User for brevity) should contain only a minimal amount of information
needed for authentication, and any information that would be needed
regularly on every request.

Ideally, the User 'type' would be made a part of the User Profile, which
can be recalled quickly via a 1to1 FK relationship (
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-the-existing-user-model
).

One of the few cases where this information could be stored directly on the
User is when the authorization system (permissions and user tests for
access) are contingent on the 'type' of user you are examining, which
effectively turns it into a role. However, unless you have a static
(unchanging) list of user roles (which is a more apropos description
anyway), you may still consider using a M2M relationship to a role table
and update the User manager to automatically join the two tables when users
are queried.

Having separate 'user' models as you've mentioned leads to the exact issue
you've brought up. If you are trying to find a user, you need a separate
query for every type of user you have in every location where you need a
list of users or perform searching for specific users. This causes an
artificial and unnecessary inflation of the number of queries run for each
request, and complicates the code trying to parse multiple sets of results.

Proxy models may also be an alternative (
https://docs.djangoproject.com/en/1.11/topics/db/models/#proxy-models).
However, those can be a bit unwieldy to handle and present the same issues
as pulling a user list.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWkc8XrQEnS9ET12Y%3D5nToFV29QTqHwOrcPMX_rvEM17g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to