Django's best user model design

Some of you will probably say that this is a recurring topic, but after reading many articles, it still seems very controversial to me. My question is about the best way to use and extend the User model keeping the authentication mechanisms (and others) available in Django. However, I prefer to describe my design:

  • There are users (patients) who can register by providing basic information (first name, last name, date of birth, gender, email address, password). Preferably the email should replace the username.
  • When the patient is in the app, he can register a new patient (imagine a family member), but no email and password are required because they will not be logged in.

In the first part, the Django doc suggests extending User with a OneToOne relationship to a profile. However, in order to replace the username by email, they suggest then creating a custom user extending from AbstractUser as well as an associated UserManager. The second requirement is similar to a one-to-many relationship from users to users. So, according to Django, which should be the best strategy: creating an entirely new user model and a one-to-many user-user adding a specific attribute that differentiates the main users and family members? OR a Django user extension with a profile followed by a one-to-many relationship profile profile? Which option best preserves the benefits of user authentication and Django administration?

Thanks for any comment, suggestion, example.

+3


source to share


1 answer


First, if you want to use email as your username, use the functional function Django custom user

. It works well. Then note that this is not because you have created your own user that you cannot extend with your profile.

So, a good solution might be:

  • Create a custom Django user without trying to add specific fields to it (sole purpose here is to use email for login instead of username).
  • Create a class PatientProfile

    that has a one-to-one relationship (empty = True) with the User class.


Thus, a patient who can log on will be associated with a user instance and will use that instance for this purpose. On the other hand, a patient who cannot log in will not be associated with any user instance.

After all, there is no problem using OneToMany relationship with PatientProfile for what you want to do.

+2


source







All Articles