1

Firstly I'm coming from a PHP background and new to Django. And I'm trying to upgrade a webapp from a PHP framework to Django, keeping the database unchanged.

The built-in authentication in Django is bit confusing, as my User table is different to what is expected in django's auth.User modal.

In my usecase, I have an Employee entity whose login account is created at the instance of creating the Employee itself.

The authentication part I managed to complete by implementing a custom AuthBackend.

But now, I'm unable to use the @login_required decorator for other views post-login, because it throws and error 'User' object has no attribute 'is_authenticated'.

In order to use is_authenticate method my User modal must then extend from auth.AbstractUser or auth.AbstractBaseUser.

How can I keep the original table structure for User modal, but still get around these authentication functions in django?

---- Update to question ---- This is my user modal.

class User(models.Model):
    employee = models.ForeignKey(Employee, models.DO_NOTHING)
    user_role = models.ForeignKey(UserRole, models.DO_NOTHING, blank=True, null=True)
    username = models.CharField(max_length=50, blank=False, null=False)
    password = models.CharField(max_length=50, blank=True, null=True)
    created_on = models.DateTimeField()
    last_updated_on = models.DateTimeField(blank=True, null=True)
    last_password_change = models.DateTimeField(blank=True, null=True)
    status = models.IntegerField(db_comment='0-Deleted, 1-Active, 2-New')
    last_login  = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user'
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
crishym
  • 23
  • 5
  • can you share your custom user model? – Willem Van Onsem Jun 25 '23 at 13:30
  • I did not user a custom User Model. I only wrote a custom AuthBackend by Extending from django.contrib.auth.backends.BaseBackend – crishym Jun 25 '23 at 13:50
  • well the error says there is no `.is_authenticated` attribute, for the builtin user model, there is such attribute, so that means that your backend either does not return a `User` model but something similar, or you use a user model without an `is_authenticated` attribute. – Willem Van Onsem Jun 25 '23 at 14:01
  • In you question you also post a custom user model, so it is not entire clear how that makes sense. – Willem Van Onsem Jun 25 '23 at 14:02

1 Answers1

0

A simple way to fix this is just set is_authenticated to True at your custom user model, like:

class User(models.Model):
    is_authenticated = True
    employee = models.ForeignKey(Employee, models.DO_NOTHING)
    # …

That being said, working from the AbstractBaseUser often will automate a lot of the settings. You can still override most functionalities anyway.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555