I want to fill the array 'v' with the number of doctors in each department, I got this error :
v[i]=Doctor.objects.all().filter(idDept__deptName__contains=s.deptName).count()
~^^^
IndexError: list assignment index out of range
views.py
def accueilAdmin(request):
if not request.user.is_staff:
return redirect('loginAdmin')
depts=Department.objects.all()
v=[]
i=1
for s in depts:
v[i]=Doctor.objects.all().filter(idDept__deptName__contains=s.deptName).count()
i+=1
models.py
class Department(models.Model):
deptName = models.CharField(max_length=50, null=False)
...
return self.deptName
class Doctor(models.Model):
name= models.CharField(max_length=50)
...
idDept = models.ForeignKey(Department, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.name
I don't know how to solve it, I would appreciate your help, thank you.