1👍
Here is an Example.It will be used in case or email or phone number
try:
user_obj = User.objects.get(Q(email=email_or_mobile) | Q(mobile_number=email_or_mobile))
except User.DoesNotExist:
raise Exception("Invalid email or mobile number")
if not user_obj.check_password(password):
raise Exception("Invalid password")
if user_obj.is_active:
login(request,user)
0👍
Django enforce you to have a unique USERNAME_FIELD
in User model in order to use all the features that it provides out of the box. But you can always customize it if you really need it (with a little extra work).
In your situation you need a User model with two fields that uniqueness define them. This implies having a Custom User model and implementing your custom Authentication Backend
For User model you should have something like this:
class User(AbstractUser):
username = None
employee_no = models.IntergerField()
company_name = models.CharField(max_lenght=100)
USERNAME_FIELD = "employee_no" # Not really used, but required by Django
# Completes custom User subclassing ...
class Meta:
# This is required for your design in order to have database integrity
unique_together = ('employe_no', 'company_name')
If your try to run server with this user model you will get System check errors
You should add to your settings
SILENCED_SYSTEM_CHECKS = ["auth.E003", "auth.W004"]
Now you have to implement a custom auth backend in order to authenticate
users against two fields. For that just subclass django.contrib.auth.backends.BaseBackend
and implement your authentication logic.
Something like this:
class CustomBackend(BaseBackend):
def authenticate(self, request, employee_no=None, company_name=None, password=None, **kwargs):
if employee_no is None or company_name is None or password is None:
return
try:
user = UserModel._default_manager.get(employee_no=employee_no, company_name=company_name)
except UserModel.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a nonexistent user (#20760).
UserModel().set_password(password)
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user
And add this backend to your settings in the list of AUTHENTICATION_BACKENDS
(dotted string path)
Finally you must ensure passing the right parameters in login view to authenticate method and be very carefull with other integrations that requires standar Django User model.
- [Answered ]-Is there a way to not return a response from a Django view?
- [Answered ]-Wsgi.py error after migrating GAE app to Python 2.7
- [Answered ]-Pragmatically adding give-aways/freebies to an online store