views:

42

answers:

2

Hay all, I'm having difficulty accessing the first related object within a template.

I'm using

{{ course.student_set.all[0].get() }}

but its throwing loads of errors. How do i get the first related object?

Thanks

+3  A: 

{{ course.student_set.all.0 }} Will do the trick for you, but I wonder why you want to only get the first student from your course in your template.

Will Hardy
Well i wanted to get a random student. But the first will be good enough.
dotty
A: 

Try {{ course.student_set.all.0 }}. Template engine does not allow arbitrary code and method access syntax is different from normal Python code. I recommend reading this overview to understand how templates work

Alex Lebedev