You can use the Django custom model , add a type field, and use it as a factory source to load Vendor / Customer objects. This solution worked very well for me. Where to begin:
models.py
from django.contrib.auth.models import AbstractUser TYPES = ( ('Vendor', 'Vendor'), ('Customer', 'Customer'), ) class MyUser(AbstractUser): type = models.CharField(max_length=10, choices=TYPES, default='Customer')
This approach is very flexible and scales well if you need to add other types in the future. A small example of a forwarding approach:
class Base(): def __init__(self, auth_user): self.user = auth_user def redirect_to(self): return "" class Vendor(Base): def redirect_to(self): return "/login/vendor" class Customer(Base): def redirect_to(self): return "/login/customer"
Then, in the login view, you simply dynamically create a user object:
auth_user = form.get_user() cls = globals()[auth_user.type] user = cls(auth_user)
You can easily create other types of users by simply adding a new class and applying the necessary methods without even touching the rest of the code.
source share