0

i have the following views.py:

from django.template import loader
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
from student.models import CustomUser
from student.forms import UserForm
from django.template.context_processors import csrf
from django.shortcuts import render_to_response
from django.contrib.auth.signals import user_logged_in
from .models import UserSession

def user_logged_in_handler(sender,request,CustomUser,**kwargs):
    UserSession.objects.get_or_create(
        user=CustomUser,
        session_id= request.session.session_key
        )
user_logged_in.connect(user_logged_in_handler)

def delete_user_sessions(CustomUser):
    user_sessions=UserSession.objects.filter(user=CustomUser)
    for user_session in user_sessions:
        user_session.session.delete()

i have the following models.py:

class UserSession(models.Model):
user= models.ForeignKey(settings.AUTH_USER_MODEL)
session=models.ForeignKey(Session)

Also in models.py i have class CustomUser(AbstractUser). What's wrong in my views.py as the error says when i try to access /admin/

user_logged_in_handler() takes exactly 3 arguments (2 given) 

The full models.py is as follows: //models.py

from django.utils import timezone
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
from django.contrib.sessions.models import Session

class CustomUser(AbstractUser):


    addr1= models.CharField(max_length=20)
    addr2= models.CharField(max_length=20)
    city= models.CharField(max_length=20)
    state= models.CharField(max_length=20)
    country= models.CharField(max_length=20,choices=country_choices)
    pincode= models.IntegerField(default=0,blank=True,null=True)
    securityq= models.CharField(max_length=20)
    securitya= models.CharField(max_length=20)

class userresp(models.Model):

    uid=models.ForeignKey(settings.AUTH_USER_MODEL,blank=True,null=True)
    resp=models.CharField(max_length=20)
    datetime=models.DateTimeField(default=timezone.now)

    def __unicode__(self):
        return u"{} {}".format(self.uid,self.datetime)

    class Meta:
        db_table="userresp"

class UserSession(models.Model):
    user= models.ForeignKey(settings.AUTH_USER_MODEL)
    session=models.ForeignKey(Session)
Shefali
  • 251
  • 6
  • 22

1 Answers1

0

As you have a CustomUser model, you need to register it as the sender.

def user_logged_in_handler(sender,request, user, **kwargs):
    UserSession.objects.get_or_create(
        user=user,
        session_id= request.session.session_key
    )
user_logged_in.connect(user_logged_in_handler, sender=CustomUser)

Check more details in Django auth signal doc

Md. Al-Amin
  • 1,423
  • 1
  • 13
  • 26
  • now it gives this error...Cannot assign "": "UserSession.user" must be a "CustomUser" instance. – Shefali Sep 21 '17 at 17:28
  • still the above error...ValueError at /admin/login/.....Cannot assign "": "UserSession.user" must be a "CustomUser" instance – Shefali Sep 21 '17 at 17:36
  • Updated again. This should work. I missed the user instance before. – Md. Al-Amin Sep 21 '17 at 17:46
  • UserSession.user" must be a "CustomUser" instance - >user=(should be an instance of CustomUser)...how do i do that? – Shefali Sep 21 '17 at 17:47
  • now this error...invalid literal for int() with base 10: 'iie27cmjouht24y424g44s5qlm999vcj' – Shefali Sep 21 '17 at 17:51
  • Seems like it's not related to the issue. There might be something else. – Md. Al-Amin Sep 21 '17 at 17:54
  • what do you mean? – Shefali Sep 21 '17 at 17:54
  • Maybe somewhere in the code, you are trying to keep some other data type value in an integer field. Check this question for more details - https://stackoverflow.com/questions/13861594/valueerror-invalid-literal-for-int-with-base-10 – Md. Al-Amin Sep 21 '17 at 17:59
  • The new error you are getting is being caused by this line `session_id= request.session.session_key`, the value is not `int` – aspo Sep 21 '17 at 20:48