Hey,
So here's the problem. Imagine 2 models: Photographer and Photo. Strict rule is that there can be only 1 photographer for image, so in Photo there's a ForeignKey link to Photographer.
class Photographer(models.Model):
name = models.CharField(max_length = 40)
class Photo(models.Model):
name = models.CharField(max_length = 40)
photographer = models.ForeignKey(Photographer)
Now I'd like to show most famous photographers on a main page with 3 most popular his photos. So it should be something like:
# views.py
def main(request):
photographers = ...
return render_to_response("main_page.html", {
"photographers": photographers
})
#main_page.html
{% for photo in photographers.photo_set.all()[:3] %}
{{ photo.name }}
{% endfor %}
But Django templating system doesn't allow all() and [:3]. So what is the best way to do that? The most obvious solution would be to create objects on the fly (create array from photographers and photos and pass it to template), but I just don't see this a good way.
Please don't just refer me to Django manual, I read it thoroughly, but couldn't find an answer...