k alex
k alex

Reputation: 3

Django - Is it better to store user information by customising the default User model, or should I create a separate Model for this?

This is for a practice project I am building to learn Django. The site will need to track certain data about the user. Should I customise the default User model? Or create a new model like -

class UserData(models.Model):
    '''holds user info'''
    owner = models.ForeignKey(User)
    # data

I couldn't find a clear answer.

Upvotes: 0

Views: 739

Answers (2)

marcanuy
marcanuy

Reputation: 24012

Users are special so it is better to use django.contrib.auth.models.AbstractUser to keep the the default user model behaviour and add your custom fields there.

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    pass

I've wrote an article about it that I think it would be userful in this case.

Upvotes: 0

Alexandr Tatarinov
Alexandr Tatarinov

Reputation: 4044

It really depends. I would recommend keeping User model small, and put most of the additional information in some kind of Profile model. However, there are certain important things that you may want to place in the User model, for example:

  1. user_type field - you may have multiple Profile models (think CustomerProfile, VendorProfile etc.) and you need a way to distinguish User and grant appropriate access to them

  2. Something related to authorization, like require_2fa field

  3. If you are starting a new project and don't expect a lot of additional info, you may keep them in the User model just for simplicity, especially if you are already customizing it (i.e. to replace username with email)

Upvotes: 1

Related Questions