Good morning, create my own user model in Django and the idea is to use it to validate the login to the system without using the model.user that Django has by default. How can I do so that users can login to my system?
Good morning, create my own user model in Django and the idea is to use it to validate the login to the system without using the model.user that Django has by default. How can I do so that users can login to my system?
Well, the truth is I do not know why you do not want to use the django user model, in fact I think it offers you a million advantages to make your own model, and the most important one is that you will not have to design a method to save passwords safely, because this model does it for you.
Django offers you many ways to Extend the User
model so that you can have many more fields to save your users' information, among those ways is to make your user model has a one-to-one relationship with the django user model, and the other is to extend a user's base class that contains the django user's methods and create your own fields, these would be these two options:
1.Extend the User model:
from django.contrib.auth.models import User
class Empleado(models.Model):
usuario = models.OneToOneField(User)
edad = models.IntegerField()
genero = ...
fecha_nacimiento = ...
And get the User employed like this:
usuario = User.objects.get(id=id)
usuario.empleado.edad
2. Create a Model that inherits from a base user class:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
# agregas tus otros campos, esta es una forma mas directa, ya que guardas todos los datos en la misma tabla
In the second way, what you save is the query to the employee table, since you can have all the data in the same table.
In the second case, you must add this line in settings.py
AUTH_USER_MODEL = 'myapp.User'
Now, to do the effective authentication you must create a file that by convention is called 'backend.py' and you must add the following:
# debes importar tu modelo de usuario, independiente de cual sea
from yoursite.models import Usuario
class UserAuthentificacionBackend(object):
def authenticate(self, username=None, password=None):
try:
user = Usuario.objects.get(username=username)
# en este punto, debes verificar la contraseña, yo lo hare como lo hace el modelo de usuario de django, siguiendo los metodos que trae este
if user.check_password(password):
return user
except Usuario.DoesNotExist:
return None
def get_user(self, user_id):
try:
return Usuario.objects.get(id=user_id)
except:
return None
The idea of the backend is a class that inherits from object with two important methods, get_user and authenticate, which when called with the indicated parameters must return the user if there is, or not return anything in the case opposite.
Note: As you use the username
you can simply use the email, or whatever you want to do the login or authentication.
Finally, what you should do is notify Django that you will use a new backend for authentication, doing the following:
In settings.py:
AUTHENTICATION_BACKENDS = ('ruta.a.tu.backend.UserAuthentificacionBackend',)
Keep in mind that the routes are with points, where you call an app (the app must be in INSTALLED_APPS) that would be the first part of the route, and each separation in points can be a directory or a file, and the last (in this case UserAuthenticationBackend) belongs to the class within the file of the route.
If you change the user model and do not change the AUTH_USER_MODEL
the Django administrator may not work for you
Any questions, comment