Might be silly question but… how do I present pages in reverse chronological order?

889 views
Skip to first unread message

Chris Adams

unread,
Aug 30, 2016, 7:30:49 AM8/30/16
to Wagtail support
Hi all,

I'm enjoying working with Wagtail so far, but after importing a load of content from a blog, I can't see a way to have this presented in the admin in reverse chronological order.

I'd like to be able to present content in this reverse chronological order in the admin, but I'm not sure of the most 'Wagtail' way to do this:

Should I just be using modeladmin now, that's part of contrib? I can see how that would work, and it would give me a fair amount of control, but then I need to tell users to only use the explorer for static, non time-based content.

Or is there some clever script to take a load of child pages under a blog index, for example, and programatically set their order, based on their 'date' attribute? This is my preference, but I'm not familiar enough with Treebeard to know where to start.

Any pointers would be gratefully accepted…

Ta

Chris

keepingtrc...@gmail.com

unread,
Aug 30, 2016, 10:19:20 AM8/30/16
to Wagtail support
don't know if this will help, but try using:

@hooks.register('after_create_page')

Adam Cox

unread,
Aug 30, 2016, 1:25:27 PM8/30/16
to Wagtail support
Do you have Meta options defined on your page model .e.g.:

class MyPage(Page):
   
class Meta:
        ordering
= ['-date']

Matthew Westcott

unread,
Aug 30, 2016, 2:18:48 PM8/30/16
to wag...@googlegroups.com
Hi Chris,
Configurable sort order in the admin is something that's been on the todo list for a while, I'm afraid: https://github.com/torchbox/wagtail/issues/882

To fix up the ordering as a one-off task, you could do something like this:

for page in BlogPage.objects.child_of(blog_index).order_by('-date'):
page.move(blog_index, 'last-child')

However, this isn't something you should run on a regular basis. Treebeard uses fixed-length strings to represent tree positions, which means there's a finite number of slots - and these don't get re-used in move operations. That finite number is on the order of millions, but if you had a few thousand blog posts and ran this on (say) an hourly schedule, it wouldn't be inconceivable to run out of slots.

Cheers,
- Matt

Chris Adams

unread,
Nov 17, 2016, 5:41:24 AM11/17/16
to Wagtail support
Hi Matt, 

Thanks for coming back to me with this - where would I look to understand how Wagtail switches between different ordering when presenting a list of pages?

In my case, I have a single NewsIndex model, which has a number of NewsPage children, and I was able to get them displaying in order like so, by running this script.

news_index = NewsIndex.objects.all().first()
 
for ns in Page.objects.child_of(news_index).order_by('newspage__date'):
    ns.move(news_index, 'first-child')

However, I only ever see this correct chronological order when I add the listing of `?ordering=ord` here, and have a change to try arranging files.
 
This makes me think that the ordering I see when first visiting a listing of say, a News Index page's children, is some other scheme, but I'm not sure where this might be stored in order for me to change it, as part of a one-off task to run when carrying out a content import for example.

Where ought I look for this? I'm happy to update docs baed on what I've learned as I can't be the last person who might want to present pages in some chronological order for an import in future.

Thanks again for all the time put into Wagtail otherwise - it's a lovely piece of software :)

Chris



Matthew Westcott

unread,
Nov 18, 2016, 5:49:41 AM11/18/16
to wag...@googlegroups.com
Hi Chris,

Ah yes, you're right - modifying the native tree ordering (as done by page.move(parent, 'first-child') ) is of limited usefulness, given that you have to specifically switch to ordering=ord to see it...

The default page ordering in the explorer is set to latest_revision_created_at descending (i.e. most recently edited first):
https://github.com/torchbox/wagtail/blob/51b4f0f53d93a69f3beaba67e3211cd001ff4087/wagtail/wagtailadmin/views/pages.py#L48

We didn't design that with overriding in mind (that's waiting on https://github.com/torchbox/wagtail/issues/882), but I think it might be possible to make it work with the construct_explorer_page_queryset hook <http://docs.wagtail.io/en/v1.7/reference/hooks.html#construct-explorer-page-queryset> - something like:

@hooks.register('construct_explorer_page_queryset')
def apply_default_order_to_news_section(parent_page, pages, request):

if parent_page.slug == 'news' and 'ordering' not in request.GET:
# this is the news section, and the user has not chosen a specific ordering
pages = pages.order_by('first_published_at')

# note that if the field you want to order on is specific to NewsPage,
# you'll need to hack things a bit more to retrieve that:
# pages = NewsPage.objects.child_of(parent_page).order_by('post_date')

return pages


Cheers,
- Matt

Chris Adams

unread,
Nov 18, 2016, 11:08:11 AM11/18/16
to Wagtail support
Matt,

You are credit to the Django community and open source in general. This worked a treat!

Thanks again

Chris
Reply all
Reply to author
Forward
0 new messages