2

I am creating Wagtail Snippets for things like Country and State, but also for other features like Transportation Options. I am surprised that there is no ability to Order snippet display in "picker" panels.

"Snippets lack many of the features of pages, such as being orderable in the Wagtail admin" https://docs.wagtail.io/en/stable/topics/snippets.html?highlight=order

This means that if I insert (say) a new Country, it's impossible to make it appear in an alphabetic list, making it very hard for users to find in a panel.

Is there a way, at least, to render snippet items alpha-sorted in the panel?

I would really prefer to be able to order in the Admin UI, so I could have options like "On Subway", "Short Walk to Subway", "Long Drive to Subway" grouped together, rather than alpha-sorted. If I cannot do this with Snippets, is there a way to do something similar with an Orderable, and allow it to be maintained in the Admin UI like Snippets are?

Thanks.

Chris Shenton
  • 125
  • 1
  • 13
  • 1
    You might find one of these to be useful: https://pypi.org/project/wagtail-orderable/ or https://pypi.org/project/wagtail-adminsortable/ – Dan Swain Aug 18 '20 at 16:09

1 Answers1

3

Django allows you to set an ordering property on the model's Meta class to define the default ordering used by queries on that model - listings within the Wagtail admin will use this.

@register_snippet
class Country(models.Model):
    name = models.CharField(max_length=255)

    class Meta:
        ordering = ['name']

For user-defined orderings, you can define a numeric field (named position, for example) and set the ordering property to that field.

gasman
  • 23,691
  • 1
  • 38
  • 56
  • Thanks, I've used a numeric for manual sort: ```ordering = ['sort_order']``` Is there no way to get the Up/Down arrows on the snippet UI? No Panel types that provide it, like normal Models? Just manual edit of that number in the Edit form? – Chris Shenton Sep 23 '20 at 18:52
  • 5
    Is there still a way to actually order snippets, though? Because for people finding this answer in the hopes of learning how to actually make snippets orderable (without making them Page classes, which wouldn't make sense for a lot of snippets) this answer is not going to help them solve their problem =/ – Mike 'Pomax' Kamermans Oct 26 '21 at 21:33