So here is the problem. Imagine 2 models: photographer and photo. The strict rule is that there can be only 1 photographer for an image, so Photo has 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 would like to show the most famous photographers on the main page with 3 of his most popular photographs. Therefore, it should be something like:
# views.py def main(request): photographers = ... return render_to_response("main_page.html", { "photographers": photographers })
But the Django template system does not allow all () and [: 3]. So what is the best way to do this? The most obvious solution would be to create objects on the fly (creating an array of photographers and photographs and transferring it to a template), but I just don't see a good way to do this.
Please do not just refer me to the Django manual, I read it completely, but could not find the answer ...
source share