Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to create a tuple of objects create an object and a queryset
    primarykey
    data
    text
    <p>I need to iterate through a tuple in the template, but from the code i've built i am getting a tuple of an object (album) and a queryset (photo). The problem is how do i iterate over them now in the template?</p> <p>models:</p> <pre><code>class Album(models.Model): title = models.CharField('כותרת', max_length=100, db_index=True) created = models.DateTimeField('תאריך פרסום', auto_now_add=True) creator = models.ForeignKey(User, related_name='galleries_creator', verbose_name='נכתב ע"י') class Photo(models.Model): title = models.CharField('כותרת', max_length=100) album = models.ForeignKey(Album, verbose_name='שייך לאלבום') photo = models.ImageField('תמונה', blank=True, upload_to=get_image_path) photo_mid = models.ImageField('תמונה בינונית', blank=True, upload_to='images/galleries/mid/', editable=False) photo_thumb = models.ImageField('תמונה קטנה', blank=True, upload_to='images/galleries/thumbs/', editable=False) created = models.DateTimeField('תאריך פרסום', auto_now_add=True) is_landscape = models.NullBooleanField(blank=True, verbose_name='האם תמונת לנדסקייפ', editable=False) </code></pre> <p>View:</p> <pre><code>def index(request): albums = Album.objects.all().order_by('?')[:10] album_list = [] for album in albums: album_list.append((album, Photo.objects.filter(album=album).order_by('?')[:1])) return render_to_response('galleries/index.html',{'albums':album_list}, context_instance=RequestContext(request)) </code></pre> <p>template:</p> <pre><code>{% for album, photo in albums %} &lt;div class="polaroid" id="picture_{{ forloop.counter }}"&gt; &lt;img src="{{ MEDIA_URL }}{{ photo.photo }}" alt="Picture #{{ forloop.counter }}" /&gt; &lt;p class="caption"&gt;&lt;p class="title"&gt;אלבום {{ forloop.counter }}&lt;/p&gt;&lt;p&gt;{{ album.creator.first_name }} {{ album.creator.last_name }}&lt;/p&gt; &lt;/div&gt; {% endfor %} </code></pre> <p>As i see it, i can't do photo.photo because photo is a list, not an object and i really don't want to do a for loop for every photo in the tuple (there is only one photo, so this is just a waste).</p> <p>assert False album_list returns:</p> <pre><code>[(&lt;Album: אלבום שני - ממשק מנהל&gt;, [&lt;Photo: אלבום 2 תמונה 2&gt;]), (&lt;Album: אלבום ראשון - ממשק מנהל&gt;, [&lt;Photo: אלבום 1 תמונה 7&gt;]), (&lt;Album: אלבום נתן&gt;, [&lt;Photo: נסיון 4&gt;])] </code></pre> <p>What can i do in my view or template to solve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload