David Dahan
David Dahan

Reputation: 11172

Should my custom User model be an abstract class?

I created a project with a custom User model, and profiles linked to my user, this way:

class User(AbstractBaseUser, PermissionsMixin):
    # ...

class BizProfile(models.Model):
   user = models.OneToOneField(settings.AUTH_USER_MODEL)
   # ....

class CliProfile(models.Model):
   user = models.OneToOneField(settings.AUTH_USER_MODEL)
   # ....

I just realised that functionally, I create BizProfile or CliProfile, but it makes no sens to create a User only in my project.

In that case, should I make my User class abstract? What are the pro's and con's of doing this? Currently, it's not abstract but works well.

Thanks.

Upvotes: 0

Views: 124

Answers (1)

knbk
knbk

Reputation: 53699

That's not possible.

An abstract model is a model that doesn't exist in the database. For a foreign key to work, both models have to exist in the database. The data in the User model also needs to be saved to the database anyway.

It's also not possible to somehow have two separate user models (e.g. BizUser and CliUser), as all code in Django and other third-party apps expects that there is a single user model and won't work with multiple models.

Upvotes: 3

Related Questions