Tuesday, November 8, 2016

Re: Canonical way of handling multiple types of users? (Profiles vs subclassing django.contrib.auth.models.AbstractUser)

On 9/11/2016 11:54 AM, Victor Hooi wrote:
> Hi,
>
> What is the current canonical way to handle multiple user-profiles in
> Django?
>
> For example - say you have "Teachers", "Students", "Parents" - you may
> have slightly different fields for each one and/or different
> behaviour. Students will have things like grades, Parents may have
> 1-to-many Students etc.
>
> I understand you can use a OneToOne field to associated different user
> profiles with each type of user:

If a Parent is also a Teacher then you would need two profiles for that
parent. Personally, I like profiles in a OneToOne relationship with the
user (or custom user) model but for different sets of characteristics I
would use multiple tables and make each of them OneToOne with user.

If they were somewhat similar I would separate out all the common fields
and methods and make an abstract CoreFields model and inherit that in
each of the different profile models. For example
StudentProfile(CoreFields), TeacherProfile(CoreFields)
ParentProfile(CoreFields) and so on. That would let you have more than
one profile per user.

I'm not sure about canonical in this context. The zen of python is
probably your best guide.

Mike

>
> https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model
>
> The docs mention using a django.db.models.signals.post_save
> <https://docs.djangoproject.com/en/1.10/ref/signals/#django.db.models.signals.post_save>
> signal on User, but I'm guessing that won't work here if you have
> multiple types of users.
>
> Or are you better off subclassing
> django.contrib.auth.models.AbstractUser? (I get the impression using
> profile models is less invasive).
>
> Regards,
> Victor
> --
> 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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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/314de801-0ca7-4eb3-abb9-7a3044994bcd%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/314de801-0ca7-4eb3-abb9-7a3044994bcd%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/f7edf132-fc56-d1fb-06a5-bbd80e99ec17%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment