How to efficiently get specific children

361 views
Skip to first unread message

g

unread,
Aug 20, 2014, 11:22:26 AM8/20/14
to wag...@googlegroups.com
How can I efficiently get specific children?

If I understand correctly from reading the source code, the following runs a query per page:

def specific_child_pages(self):
        all_pages = self.get_children().live().filter(show_in_menus=True)
        specific_pages = [page.specific for page in all_pages]
        return specific_pages

Is it possible to get specific pages for all children but only run one query per page type instead of each page?

------------------------------------------------------------

If not, maybe there is a more efficient way to accomplish the following:
------------------------------------------------------------

I am trying to create a generic index page that can display a thumbnail of the content if the page defines one.  Ideally, the poster file could be stored on the base page model, but since the page model isn't swappable I don't think this is supported?  Is there any way to accomplish this without needing the specific page?

Karl Hobley

unread,
Aug 21, 2014, 11:07:15 AM8/21/14
to wag...@googlegroups.com
 > Is there any way to accomplish this without needing the specific page?

One possible way could be to store the thumbnail in a separate model. Something like:

class PageThumbnail(models.model):
    page
= models.OneToOneField(Page, related_name='thumbnail')
    image
= models.ImageField()


You can use a page_published signal handler to update the thumbnail whenever the page is published:

from wagtail.wagtailcore.signals import page_published

@reciever(page_published)
def set_page_thumbnail(instance, **kwargs):
   
# 'instance' is the specific version of the page

   
# Get PageThumbnail object
    page_thumbnail
, created = PageThumbnail.objects.get_or_create(page=instance)
    page_thumbnail
.image = page.get_thumbnail()
    page_thumbnail
.save()


All of your pages should now be able to access their thumbnails through "mypage.thumbnail.image".

As an extra optimisation, add ".select_related('thumbnail')" to your QuerySet of pages to make it select all the thumbnail objects in one go.


Hope this helps,

Karl

g

unread,
Aug 21, 2014, 9:45:01 PM8/21/14
to wag...@googlegroups.com
Karl,

Thanks for this reply.  This is probably the best approach.  Is there any way to get something like the PageThumbnail model into the Page editor UI so the user could populate it directly?  Some of my page types can automatically provide a thumbnail, but others will require user input.

Thanks,
Gordon

Karl Hobley

unread,
Aug 22, 2014, 3:56:36 AM8/22/14
to wag...@googlegroups.com
You can register it a snippet:

from wagtail.wagtailsnippets.models import register_snippet


class PageThumbnail(models.Model):
   
...

   
# Snippets must define a __unicode__ method
   
def __unicode__(self):
       
return self.page.title


register_snippet
(PageThumbnail)

The thumbnails should then appear under the "Snippets" section in the admin,



On Wednesday, 20 August 2014 16:22:26 UTC+1, g wrote:
Reply all
Reply to author
Forward
0 new messages