Query for all Images in Collection

401 views
Skip to first unread message

siram....@gmail.com

unread,
Jun 27, 2016, 7:52:54 AM6/27/16
to Wagtail support
Hi All,

I am trying to get all Images (in a certain Collection), but can't get it to work unfortunately. I tried to query for them like this: 

def photos(self):
photos = Image.objects.all()
return photos

def get_context(self, request):
photos = self.photos

context = super(PhotosPage, self).get_context(request)
context['photos'] = photos
return context

and then use 
{% for item in photos %}

in the template. However, this is always empty.

Any hints? Also on how to get them from just one collection?

Best, Simon

Matthew Westcott

unread,
Jun 27, 2016, 12:47:56 PM6/27/16
to wag...@googlegroups.com
Hi Simon,

The line
photos = self.photos
needs to be:
photos = self.photos()

- otherwise, the 'photos' method won't get called (it'll just set the variable 'photos' to be a reference to the function instead).

To limit the query to a specific collection:
photos = Image.objects.filter(collection__name='Photos')
(or, if you want to identify the collection by ID rather than name: filter(collection__id=1) - see https://docs.djangoproject.com/en/1.9/ref/models/querysets/#id4 for other ways of querying.)

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/05df8e06-463e-4c33-b70c-c89f7abf6bde%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages