I have a for loop such as:
staff = Staff.objects.all().order_by('person__full_name')
for k in staff:
categ = k.staff_job_categories.all()[1]
if categ.name == unicode("xxx","utf-8"):
t = categ.name
But for categ = k.staff_job_categories.all()[1]
I get an List out of index Error.
Any ideas?
Seems
print len(k.staff_job_categories.all())will output0or1because there are no staff job categories for this item or just only one category exists (which has index0and not1).So if you’re interesting exactly in second category if it exists your code can be changed to