Search code examples
pythondjangodjango-modelsdjango-usersdjango-1.6

Setting up two different types of Users in Django 1.5/1.6


Please note--this is an updated version of my original question on this subject, but deserves to be asked again with the change in how Django deals with users and authentication.

I'm working on a website with two very different kinds of users--let's call them Customers and Store Owners. Both register on the site, but have very different functionality. Customers simply have a single profile and can shop among the stores that they like. Store Owners have a single account but can have access to multiple stores, and each store can have multiple Store Owners.

The exact details of the models don't matter, but the two types of users would require very different fields. The models ideally would look something like this:

Customer
  email (username)
  password
  name
  address
  time_zone
  preferred_shipping
  favorite_stores (many-to-many field)
  ...

Store Owner
  email (username)
  password
  name
  balance
  stores_owned (many-to-many field on Stores)
  stores_managed (many-to-many field on Stores)
  ...

Originally, when Django had poor custom user support, I had a UserProfile class with some additional fields with a OneToOne on User, and then additional Customer and StoreOwner classes that were OneToOne on UserProfile. This didn't work very well.

Given the changes in Django 1.5/1.6, I'm trying to come up with the best way to structure this. Right now, I have the following:

class CustomerUser(AbstractBaseUser):
    ...

class StoreOwnerUser(AbstractBaseUser):
    ...

But because there would be two types of user, I can't set AUTH_USER_MODEL to only one of them.

What is the best way to structure this so that I can have two different types of users with different fields, without causing me any problems in user authentication, user creation, or the admin?

Also, how will I be able to tell from login alone whether this user is a CustomerUser or a StoreOwnerUser?


Solution

  • It seems like there are some common features and uncommon features to your user types. If there are common features in your user types that Django's default User model doesn't support out of the box, you should subclass it directly.

    Adding in extra, uncommon features to your user types are best done not by subclassing but by using a profile. My rationale for this is because your authentication for these user types doesn't fundamentally change, but details about the user does depending on the type of user it is. To accomodate this, you create a separate model with these details and reference your User class as a OneToOne/ForeignKey relationship (depending on your design).

    You can make modifications to your user creation process to identify what kind of user type it should be, and set its associated OneToOneField/ForeignKey (depending on your design) to the appropriate customer type model.

    By doing it this way, you should only have one AUTH_USER_MODEL, and you should be able to handle details for your different customer types.