Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 619
jdotjdot Avatar asked Dec 26 '13 04:12

jdotjdot


People also ask

Can I have two user models in Django?

You can have in your models two user classes that extend from the USER model.

How do I categorize users in Django?

Groups: Way of Categorizing UsersDjango provides a basic view in the admin to create these groups and manage the permissions. The group denotes the “role” of the user in the system. As an “admin”, you may belong to a group called “admin”. As a “support staff”, you would belong to a group called “support”.


3 Answers

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.

like image 106
tsurantino Avatar answered Oct 25 '22 16:10

tsurantino


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?

You actually only have one type of user. Just that some users have specific properties set and others do not. Consider how django has "users" and "admins". They are the instances of the same model, but with different properties and permissions.

You should approach it similarly. Have one user model for your entire application. You can set properties/methods in your custom user class to identify what flags this user has set (which would determine the "type" of user there is).

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

You can use the user_passes_test decorator, which takes an argument that is a function name and will only process the view if the function returns a truth value.

like image 29
Burhan Khalid Avatar answered Oct 25 '22 18:10

Burhan Khalid


  1. Create a BaseUser which extends Django's Abstract base User
  2. Create two Sub Classes Named CustomerUser and StoreOwnerUser which extends BaseUser

    from django.db import models
    from django.contrib.auth.models import AbstractUser
    
    class BaseUser(AbstractUser):
        # all the common fields go here, for example:
        email = models.EmailField(max_length=10,unique=True)
        name = models.CharField(max_length=120)
    
    class StoreOwnerUser(BaseUser):
        # All Store Owner specific attribute goes here
        balance = models.some_balance_field()
        stores_owned = models.some_stores_owned_field()
    
        class Meta:
        verbose_name = 'Store Owner'
    
    class CustomerUser(BaseUser):
        # All Customer specific attribute goes here
        customer_id = models.CharField(max_length=30, unique=True)
        address =  models.some_address
        time_zone = models.something...
        ...
    
        class Meta:
            verbose_name = 'Customer'
    
like image 1
Kashif Siddiqui Avatar answered Oct 25 '22 16:10

Kashif Siddiqui