From 69c7945f0ebf6e64ff5559b6e3ef085261e9efd6 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Sun, 19 Jan 2020 21:49:11 +0100 Subject: [PATCH] Let new publications be first to display Overwrite wagtailorderable.models.Orderable.save() to initialize new objects with a lower sort order than existing objects, so that the new objects appear at the top of the list. --- mpicms/publications/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mpicms/publications/models.py b/mpicms/publications/models.py index e794069..5e157e9 100644 --- a/mpicms/publications/models.py +++ b/mpicms/publications/models.py @@ -10,9 +10,16 @@ from modelcluster.models import ClusterableModel +class LatestFirstOrderable(Orderable): + def save(self, *args, **kwargs): + if self.pk is None: + self.sort_order = self.__class__.objects.aggregate(models.Min('sort_order'))['sort_order__min'] - 1 + super(Orderable, self).save(*args, **kwargs) + class Meta: + abstract = True @register_snippet -class Publication(index.Indexed, ClusterableModel, Orderable): +class Publication(index.Indexed, ClusterableModel, LatestFirstOrderable): title = RichTextField(_('title'), features=['bold', 'italic', 'link']) groups = RichTextField(_('groups'), features=['bold', 'italic', 'link'], blank=True) authors = RichTextField(_('authors'), features=['bold', 'italic', 'link'], blank=True)