Search code examples
djangodjango-authentication

Custom user model vs. additional model


Due to lack of knowledge, when faced with the need to store additional information about users, I created additional UserInfo model and referenced it by foreign key to the standard User model.

I have since learned two things:

  1. It is recommended to have a custom user model.
  2. It is tricky to switch mid-project (this being the best way so far as far as I understand).

I would like to understand:

  1. If the fields in UserInfo are not used for authentication, is there any reason to merge those into the user model?
  2. What do I stand to gain by taking the time to abandon my current approach and switch to a custom user model?

Solution

  • If the UserInfo model is not required for authentication, then there's no need to create a completely custom User model.

    What you're currently doing is fine.

    But instead of using ForeignKey, use OneToOneField so that one UserInfo instance is tied to one and only one User instance.

    This method is also shown in the documentation: https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#extending-the-existing-user-model.