Django best user model design

683 views Asked by At

Probably some of you would tell that is a recurrent topic, but after reading many articles, it still seems very ambiguous to me. My question is about the best way to use and to extend the User model preserving the authentication (and others) mechanisms available in Django. However, I prefer to describe my design:

  • There are users (Patients) that can sign up providing basic info (first name, last name, birth date, gender, email, password). Preferably, email should replace the username.
  • When a Patient is in the application, it can register a new Patient (imagine a member of the family), but email and password are not required because they won't log into the system.

For the first part, Django doc propose to extend User with a OneToOne relation to a Profile. However, to replace username by email they propose then to create a custom User extending from an AbstractUser, as well as an associated UserManager. The second requirement is like doing a one-to-many relation from users to users. So, according to Django, which should be the best strategy: creating a completely new user model and the one-to-many user-user adding an specific attribute that distinguish between main users and family members? OR extending Django User with a Profile and then a one-to-many relation profile-profile? Which option preserves the best the benefits of Django user authentication and model administration?

Thank you for any comment, suggestion, example.

1

There are 1 answers

1
David Dahan On

First, if you want to use email as username, use the Django custom user functionnality. It works well. Then, note that it's not because you created your own User that you can't extend it with a Profile.

So, a good solution could be :

  • Create a Django custom User without trying to add specific fields to it (the one and only purpose here is to use email to log instead of username).
  • Create a PatientProfile class that have a one-to-one relatioship (blank=True) with User class.

This way, a patient that can log in will be related to a User instance and will use this instance for this purpose. On the other hand, the patient who can't log in won't be related to any User instance.

In the end, there's no problem to use OneToMany relationship with PatientProfile for what's you want to do.