Using a custom user model extended from AbstractBaseUser, the user object is lose between requests

48 views
Skip to first unread message

L

unread,
Sep 25, 2018, 6:49:07 PM9/25/18
to Django users
I'm using a custom user model that extends `AbstractBaseUser`. This is the user model:
class cUser(AbstractBaseUser):
   
def save(self, *args, **kwargs):
       
pass
    role_id
= models.IntegerField()
    user_id
= models.IntegerField()
    email
= models.CharField(max_length=40)
    password
= models.CharField(max_length=40)
    f_name
= models.CharField(max_length=40)
    l_name
= models.CharField(max_length=40)
    address_id
= models.IntegerField()
    phone_num
= models.IntegerField()
    loan_item_count
= models.IntegerField()
    id
= models.IntegerField(unique = True, primary_key = True)

   
def __init__(self, dictionary, *args, **kwargs):
       
self.role_id = int(dictionary['role_id'])
       
self.user_id = dictionary['user_id']
       
self.email = dictionary['email']
       
self.password = dictionary['password']
       
self.f_name = dictionary['f_name']
       
self.l_name = dictionary['l_name']
       
self.address_id = dictionary['address_id']
       
self.phone_num = dictionary['phone_num']
       
self.loan_item_count = dictionary['loan_item_count']
       
self.id = self.user_id

    USERNAME_FIELD
= 'user_id'
   
class Meta:
        managed
= False

I don't want the model to affect the DB in any way. I'm loading it by a simple raw SQL query from a gateway method.

This is how I'm handling login:


def login_request(request):
   
if request.method == 'POST':
        form
= LoginForm(request.POST)
       
if form.is_valid():
            username
= request.POST['username']
            password
= request.POST['password']
            user
= userGateway(username,password)
           
if user is not None:
               
print("=========USER==========")
               
print(user.email)
                user
.backend = 'django.contrib.auth.backends.ModelBackend'
                login
(request,user)
               
print(request.user.is_authenticated)
               
if user.role_id==1:
                   
return render(request, 'biblioteca/admin/landing.html')
                   
# return HttpResponseRedirect('/')
               
else:
                   
return render(request, 'biblioteca/landing.html')
           
else:
               
print("=========NOT USER==========")
   
else:
       
if(request.user is not None and not request.user.is_anonymous):
           
return render(request, 'biblioteca/admin/landing.html')
        form
= LoginForm()
       
return render(request, 'biblioteca/login.html', {'form': form})

As you can see, I'm setting the back-end before login to authenticate without having to pass through a password - the password check is being done when the user object is created by comparing the password passed in with the password retrieved from the DB. The first check,

print(request.user.is_authenticated)
returns true, as expected. If I then return a render, the user object is still there. However, if I leave this page, or if I return a redirect instead, the user object becomes anonymous.


Any help with this would be greatly appreciated.

L

unread,
Sep 25, 2018, 7:46:25 PM9/25/18
to Django users
As an additional detail - This product unfortunately has to use an existing database, which means no django migrations, which means all ORM has to be done manually. This is why the user model isn't capable of saving back to the DB, and why I'm going through this whole convoluted setup.
Reply all
Reply to author
Forward
0 new messages