title: Looping through two objects in a Django template
body: <p>I have an app objects and image objects which are linked to one another (the apps have images).</p>
  
  <pre><code>def index(request):
      latest_apps_list = App.objects.all().exclude(approved=False).order_by('name')[:20]
      app_images = Image.objects.filter(app__in=latest_apps_list).order_by('app__name')[:20]
      t = loader.get_template('apps/index.html')
      c = Context({
          'latest_apps_list': latest_apps_list,
          'app_images': app_images
      })
      return HttpResponse(t.render(c))
  </code></pre>
  
  <p>Now I want to loop through those images in my template. How would I do that with <strong>both</strong> variables? I tried using zip(), but this returned mysql errors as it calls for unsupported db queries. Is there another way?</p>
  
  <p>Currently I have:</p>
  
  <pre><code>{% for app in latest_apps_list %}
  
  ...{{ app.name }}
  
  {% endfor %}
  </code></pre>
  
  <p>This works. Of course, it doesn't return the images urls. (I'm using sorl-thumbnail btw.)</p>
  
  <p><strong>UPDATE</strong> Perhaps I'm just going about doing this the wrong way. Here's how I have my <strong>model</strong>:</p>
  
  <pre><code>class App(models.Model):
      name = models.CharField(max_length=200)
      # ...
  
  class Image(models.Model):
     app = models.ForeignKey(App)
     image = models.ImageField(upload_to = "apps")
  </code></pre>
  
  <p>And my <strong>view</strong> is in the original part of the post above. It seems like I should somehow be making the app's properties and the image properties all one thing, without the need to zip in the view. Is this possible?</p>
  
  <p><strong>UPDATE 2</strong> I solved this by greatly simplifying how the model is created. Here's what I did in case anyone else is trying to do this.</p>
  
  <p><strong>apps/admin.py</strong>: the image object is included as an ordinary field.</p>
  
  <pre><code>class AppAdmin(admin.ModelAdmin):
     fieldsets = [
        ('Basic', {'fields':['name','desc','price','approved','image']}),
        ('Author',        {'fields':['docs_url', 'preview_url']}),
     ]
     list_display = ('name', 'desc', 'price', 'approved')
  
  
  admin.site.register(App, AppAdmin)
  </code></pre>
  
  <p><strong>apps/models.py</strong>: Just make image part of the app itself. No foreign keys needed.</p>
  
  <pre><code>class App(models.Model):
      name = models.CharField(max_length=200)
      # ...
      image = models.ImageField(upload_to = "apps")
  </code></pre>
  
  <p><strong>apps/views.py</strong>: Now the view just has one object to loop through. No weird sql queries needed.</p>
  
  <pre><code>def index(request):
      latest_apps_list = App.objects.all().exclude(approved=False).order_by('name')[:20]
      t = loader.get_template('apps/index.html')
      c = Context({
          'latest_apps_list': latest_apps_list,
      })
      return HttpResponse(t.render(c))
  </code></pre>