On 5/02/2020 10:14 am, Zameer Ahmed wrote:
> Thanks Mike for taking time off to read long question and response is
> much appreciated.
> In all the discussion you just told me what not to do for laying down
> my model structure.
> Now I have idea somewhat not to do.
> Can you kindly laydown minimal structure where I can achieve multiple
> user roles?
There are as many different ways to do that as there are use-cases.
For my purposes (not necessarily anyone else's) I have a single user
with a user_profile to store 1:1 information such as preferences when
using the software or cellphone for MFA or (in my case) their company.
That is interesting because I decided early that for security reasons a
single login would only ever work for a single company. My software is
tenant-based with multiple companies. Users who work for multiple
companies must have multiple logins.
My basis for this is it seems to me to be the simplest and most flexible
for my purposes. Also, it means I can lift that entire section of my
code and use it virtually unchanged in other projects.
Everyone who uses the software is a human and all that is different
is(are) the role(s) they inhabit. In my software people have defined
roles, they can switch to have other roles and they can have multiple
roles at the same time.
The roles are named django_auth_groups which each get appropriate
permissions. A user needs to be in multiple groups if they need more
permissions than are available in a single group.
I have no users with their own permissions. They can only get their
permissions from group membership. I have omitted user individual
permissions entirely from the project. They are evil.
Then in a utils module I have a bunch of easy to remember functions like
this ...
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
def is_admin(user, name="admin"):
return is_member(user, name)
def is_author(user, name="author"):
return is_member(user, name)
def is_authority(user, name="authority"):
return is_member(user, name)
def is_consumer(user, name="consumer"):
return is_member(user, name)
def is_editor(user, name="editor"):
return is_member(user, name)
def is_manager(user, name="manager"):
return is_member(user, name)
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
def is_member(user, name):
if user and name and user.is_active and user.is_staff:
return user.groups.filter(name=name).exists()
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
I also have similar methods on the user model itself which return
booleans. Also one which returns a string of all the groups a user is a
member of.
So my experience may not suit your use-case but it suits me because it
is simple and flexible.
I have not extended the custome user much beyond extra methods. No extra
fields. I think a user_profile in a 1:1 relationship offers all that you
probably need for the stuff which is exclusively human-user-related.
Different user profile tables for different roles might be exactly what
you need or they might be overkill.
Maybe the way to think about it is to abstract the commonalities into a
single (ie simpler) user_profile table and then really examine the
differences and decide how best to deal with them in the context of what
you want the software to do.
That might be roles the way I do it. It might be multiple profile tables
which inherit from the common one.
I sometimes wonder how I might explain my project design to someone who
is taking it over and that usually stops all my complex ideas and forces
me to look for something closer to the real world which will be easier
to explain and if you code with real world language it will be easier to
hand over. And to understand the next time you look at it yourself!
Good luck
Mike
> <mailto:
django-users%2Bunsu...@googlegroups.com>
> > <mailto:
django-users...@googlegroups.com
> <mailto:
django-users%2Bunsu...@googlegroups.com>>.
> <mailto:
django-users%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/django-users/84e69a79-d0bc-7fe4-8a6b-e757352c27ee%40dewhirst.com.au.
>
https://groups.google.com/d/msgid/django-users/CAAUw0KoP_uMPyMv7aXrA1iwW0ESCwFqn2f8RHnvQR1k9W9%3DMQw%40mail.gmail.com
> <
https://groups.google.com/d/msgid/django-users/CAAUw0KoP_uMPyMv7aXrA1iwW0ESCwFqn2f8RHnvQR1k9W9%3DMQw%40mail.gmail.com?utm_medium=email&utm_source=footer>.