[Django]-Django authentication with modelform

2👍

You could use Django custom user model, add the type field and use it as a factory source to load Vendor/Customer objects. Such solution worked for me very well. Something to start from:

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. Small example of redirection 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 would just dynamically create the user object:

auth_user = form.get_user()
cls = globals()[auth_user.type]
user = cls(auth_user)  # This will return either Vendor or Customer object
return HttpResponseRedirect(user.redirect_to())

You can easily create another user types by just adding new class and implementing the needed methods without even touching the rest of the code.

👤matino

2👍

The best strategy for me is to write two Authentication Backends, one that enables the vendor authentication and another that does it for the customer.

AUTHENTICATION_BACKENDS is the settings constant giving the authorization backend tuple.
here can you check the documentation about authentication backends.

Declare your implentations with the settings parameter. The order counts django will try all backends untill one is successful.

Leave a comment