2

I have extended the user model to a model class 'Student' using OneToOne relation. Now I want to filter all active students with a field in the 'Student' class such as 'name'.

Tried this in django shell:

Student.objects.filter(user.is_active == True)
Traceback (most recent call last):
 File "<console>", line 1, in <module>
NameError: name 'user' is not defined

my View:

def entry(request):
     user = request.user
     if request.user.is_active:
        print("Already logged in")
        return redirect('home')
     else:
        return render (request, 'entry.html')

my Models:

class Student(models.Model):
     name = models.CharField(max_length=200)
     branch = models.CharField(max_length=200, choices=BRANCHES)
     sem = models.CharField(max_length=200, choices=SEMESTERS)
     reg_no = models.CharField(max_length=200, unique = True)
     balance = models.IntegerField(default=0)
     pin_no = models.CharField(max_length=200, unique = True)
     college = models.ForeignKey(Institute, on_delete=models.CASCADE )
     user = models.OneToOneField(User, on_delete=models.CASCADE)```
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Sangeeth Joseph
  • 152
  • 1
  • 8

1 Answers1

3

You can filter with:

Student.objects.filter(user__is_active=True)

or if you want to Retrieve the Student model linked to the logged in user, and that user should be active, you can work with:

Student.objects.filter(user=request.user, user__is_active=True)

This will return an empty QuerySet if the user is not a student, or if the user is not active.

You can boost the efficiency slighly by this for the request.user object, so:

if request.user.is_active and Student.objects.filter(user=request.user).exists():
    # user is a student and the user account is active
    pass
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • thanks for the reply, my question needed a bit more clarification, I actually want to add it to the ```request.user``` in views : ```def entry(request): user = request.user if request.user.is_active: --- I meant here with request.user print("Already logged in") return redirect('home') else: return render (request, 'entry.html')``` – Sangeeth Joseph May 08 '21 at 16:58
  • What I'm actually trying to do here is to prevent the user from going back to login page when he is already logged in, with that if condition. But my problem is that even if some other type of account is logged in other than student also the login page won't be shown to student. Please let me know if I can try something of the sort : ```def entry(request): ***if Student.objects.filter(request.user__is_active=True):*** print("Already logged in") return redirect('home')``` – Sangeeth Joseph May 08 '21 at 17:06
  • I have extended the user to 3 different models using One-to-One relations, Student is one of them, so I want to filter and see if the request.user is a Student type before checking it is active or not, becuase it is for student login – Sangeeth Joseph May 09 '21 at 09:26
  • @SangeethJoseph: you can check if the `Student` refers to the logged in user with `Student.objects.filter(user=request.user, user__is_active=True)`. – Willem Van Onsem May 09 '21 at 09:27