[Solved]-Setting up two different types of Users in Django 1.5/1.6

5👍

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.

3👍

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.

1👍

  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'
    

Leave a comment