Django authentication - basic question

Hello.

Using authentication system by django documentation:

This is the user model:

  class client(models.Model):
      id = models.AutoField(primary_key=True)

      password = models.CharField(max_length=200)
      email = models.EmailField()	
      email_confirmed = models.BooleanField(default=False)
      random_number= models.IntegerField(default=0)
      name = models.CharField(max_length=200, blank=True)
      last_name = models.CharField(max_length=200, blank=True)
      picture=models.ImageField(upload_to='media/', blank=True)	
      qualification=models.IntegerField(default=0)
      cantidad_ordenes_realizadas =models.IntegerField(default=0)
      cantidad_ordenes_canceladas =models.IntegerField(default=0) 
  
      def __str__(self):
          return self.email 

Can I implement something like this?

    user = authenticate(username='john', password='secret')
    if user is not None:
        # A backend authenticated the credentials
    else:
        # No backend authenticated the credentials

for client model.

    user = client.authenticate(username='john', password='secret')

Or should I use the user model?

You would want to use a custom User model.

The authenticate method is not a method on Model. (It doesn’t make sense to refer to client.authenticate, unless you create the authenticate method on client.)

Trying to use a custom user model without registering it to the system is possible, but you’ll have to supply your own middleware and backends for it. There are very few situations where that would be advisable or desirable.

Side Note: I strongly suggest you adopt the Django coding conventions and capitalize the class name.

1 Like