Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ForeignKey and Django Template

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...

like image 376
Vitaly Avatar asked Jan 23 '23 07:01

Vitaly


2 Answers

Try using the slice filter:

{% for photo in photographers.photo_set.all|slice:":3" %}

Note that the .all doesn't need to be explicitly called.

like image 122
Jeff Bradberry Avatar answered Jan 25 '23 20:01

Jeff Bradberry


I think Yuval A's almost got it, but not quite - the photos and photographers seem to be mixed up. I think the best approach would be to provide a top_photos attribute on the Photographer model which returns the top 3 (or however many) photos. Then:

{% for photographer in photographers %}
Photographer: {% photographer.name %}
Top Photos:
{% for photo in photographer.top_photos %}
{% photo.name}
{% endfor %}
{% endfor %}

I've left out any HTML formatting in the above, but hopefully the intent is clear.

like image 35
Vinay Sajip Avatar answered Jan 25 '23 22:01

Vinay Sajip