Orderable snippets?

1,640 views
Skip to first unread message

Lev Kanter

unread,
May 27, 2015, 4:19:29 PM5/27/15
to wag...@googlegroups.com

Hi,

does Wagtail provide a mechanism to make Snippets orderable in the admin (using a similar UI to what's used to reorder pages)?
I realize snippets aren't necessarily intended to be orderable, but I have a use case where it actually would be useful to provide a UI to let the admin control the sort order of snippets (exposing the sort _order as an editable field on the snippet model would be a cumbersome way to have the admin control the overall ordering).

For example, suppose I have a Category snippet (some page types in my app can be associated with a Category), and part of the public-facing site will need to display a list of these categories (manually ordered, as opposed to alphabetically or something). I would envision implementing something along these lines:


@register_snippet
class Category(Orderable):
    name
= models.CharField(max_length=255)

    panels
= [
       
FieldPanel('name')
   
]


class CustomPage(Page):
   category
= models.ForeignKey(Category, related_name='pages')

CustomPage.content_panels = Page.content_panels + [
   
SnippetChooserPanel('category', Category)
]


Is it possible to incorporate the ordering UI into the snippet listing UI in the Wagtail admin? Or...am I approaching this whole thing wrong?

thanks,

Lev

Brett Grace

unread,
May 29, 2015, 3:37:43 PM5/29/15
to wag...@googlegroups.com
The UI for Orderable works objects that are related to another entity (in other words, something is Orderable with respect to a container or parent object). The easiest way to achieve what you is to make another snippet, something like "CategoryMenu", that has a relationship to an Orderable object like your Category.

I did something similar to create arbitrary menus here: https://gist.github.com/bgrace/a5216b4059007924a75c

Lev Kanter

unread,
May 29, 2015, 5:43:00 PM5/29/15
to wag...@googlegroups.com
Thanks for the tip, Brett. Totally hadn't thought of that-- that's pretty awesome!

YusufSalahAdDin

unread,
Jun 9, 2015, 12:00:06 AM6/9/15
to wag...@googlegroups.com
I think that needs a html template, can you do it? and, can you put here a screen show?

Brett Grace

unread,
Jun 9, 2015, 5:26:28 PM6/9/15
to wag...@googlegroups.com
This uses all of the default admin stuff as far as I remember. I didn't need to to provide any extra HTML to make this work. I coded it for an earlier version of Wagtail, probably 0.4 or so. However I expect it would still work with recent versions of Wagtail.

What sort of defect are you seeing?

Or do you mean, HTML to make it work in your own templates? I did it with a custom tag:

@register.assignment_tag(takes_context=False)
def load_site_menu(menu_name):
    menu
= NavigationMenu.objects.filter(menu_name=menu_name)

   
if menu:
       
return menu[0].menu_items.all()
   
else:
       
return None

Then in your template:

{% load_site_menu "footer" as footer_menu_items %}
{% for menu_item in footer_menu_items %}
<li>
    <a href="{{ menu_item.url }}">{{ menu_item }}</a>
</li>
{% endfor %}

You have to create a NavigationMenu named "footer" in the admin for this to work.

There are a bunch of ways to do this, you can also get the menu in an individual page and set it into the page context, or make it a property on the page.

YusufSalahAdDin

unread,
Jun 9, 2015, 11:09:53 PM6/9/15
to wag...@googlegroups.com
Thank you very much.
Reply all
Reply to author
Forward
0 new messages