When a doctor is logged in with his email and password, his dashboard shows him some statistics: the number of his patients, the number of his appointments (including past and future appointments)
I need to filter by the doctor's email (to get the appointments/patients of that specific doctor), but I couldn't get a result of any of the three filters. I tried with request.user.id and request.user.username I didn't get anything.
Here's my code : views.py:
def Home(request):
if not request.user.is_active:
return redirect('loginpage')
g = request.user.groups.all()[0].name
if g=='Patient':
return render(request, 'homepagePatient.html')
elif g=='Doctor':
nbrAp=Appointment.objects.all().filter(emailDoctor=request.user).count()
nbrPastAp = Appointment.objects.all().filter(emailDoctor=request.user, dateAp__lt=timezone.now()).order_by('-dateAp').count()
nbrFutAp = Appointment.objects.all().filter(emailDoctor=request.user, dateAp__gte=timezone.now()).order_by('dateAp').count()
mydict={
'nbrAp':nbrAp,
'nbrPastAp':nbrPastAp,
'nbrFutAp':nbrFutAp,
}
return render(request, 'homepageDoctor.html', context=mydict)
models.py:
class Appointment(models.Model):
nameDoctor= models.CharField(max_length=50)
namePatient= models.CharField(max_length=50)
emailDoctor = models.CharField(max_length=60)
emailPatient = models.CharField(max_length=60)
dateAp = models.DateField()
timeAp = models.TimeField()
status = models.BooleanField()
def __str__(self):
return self.namePatient + " will have an appointment with Dr. "+self.nameDoctor
class Doctor(models.Model):
name= models.CharField(max_length=50)
email = models.EmailField(unique=True)
password = models.CharField(max_length=16)
def __str__(self):
return self.name
class Patient(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField(unique=True)
password = models.CharField(max_length=16)
def __str__(self):
return self.name
Thank you for your help !
I think the problem is that you try to filter email field by id or username. Try to filter by user.email
In my opinion its better to expand default django User model, and add flag if this user patient or doctor. Then you can just check this flag and filter another related instances.