Orderable not ordering

856 views
Skip to first unread message

Scot Hacker

unread,
Oct 7, 2015, 9:01:22 PM10/7/15
to Wagtail support
Feel like I must be missing something obvious here, but not sure what. I have set up Orderable snippets for a model, and the wagtail 1.1 admin does let me reorder them just fine, but after saving the page, the snippets have not in fact been re-ordered - they're just as they were before saving. 

class StandardPageRelatedSnippet(Orderable, Snippet):
    page = ParentalKey('cms.StandardPage', related_name='snippets')
    snippet = models.ForeignKey('cms.Snippet', related_name='+')

    panels = [
        SnippetChooserPanel('snippet', Snippet),
    ]
...
StandardPage.content_panels = [
    #...
    InlinePanel('snippets', label="Related snippets"),

]

Thanks for a clue.

g

unread,
Oct 15, 2015, 1:31:10 PM10/15/15
to Wagtail support
I've hit this too working on a new navigation management snippet.  I've not had time to investigate the cause but I just wanted to mention I am observing the same problem.

Dino Luk

unread,
Nov 6, 2015, 5:58:25 AM11/6/15
to Wagtail support
Bumped on the same problem in both wagtail 1.1 and 1.2rc1 when building "gallery" snippet. Wagtail admin won't save reordered content, it saves items in initial ordering instead. I am using this code

class GalleryItem(models.Model):
    image = models.ForeignKey(
       
'wagtailimages.Image', null=True, blank=True,
       
on_delete=models.SET_NULL, related_name='+')
    source
= models.CharField(
       
max_length=255, verbose_name=_('Source'),
       
help_text=_('Author/Origin of an image.'))
    alt_text
= models.CharField(
       
max_length=255, blank=True, default='',
       
help_text=_('Text displayed in image alt tag.'))
    description
= models.CharField(
       
max_length=255, blank=True, default='',
       
help_text=_('Short description of an image'))

    panels
= [
       
ImageChooserPanel('image'),
       
FieldPanel('source'),
       
FieldPanel('alt_text'),
       
FieldPanel('description')
   
]

   
class Meta:
       
abstract = True


class GallerySnippetGalleryItems(Orderable, GalleryItem):
    item = ParentalKey(
       
'core.GallerySnippet', related_name='related_gallery_items')


@python_2_unicode_compatible
@register_snippet
class GallerySnippet(ClusterableModel):
    title = models.CharField(max_length=80, verbose_name=_('Gallery title'))
    created_at
= models.DateField(verbose_name=_('Date created'))
    panels
= [
       
FieldPanel('title', classname='col8'),
       
FieldPanel('created_at', classname='col4'),
       
InlinePanel('related_gallery_item', label='Item')
   
]

   
def __str__(self):
       
return self.title


After heavy googling for the clue/solution I came to conclusion that it worked at some point, because people were repeatedly suggesting same approach. But it is all old approach when you needed to provide Model in Inline panel definition.

Joss Ingram

unread,
Nov 6, 2015, 9:51:46 AM11/6/15
to Wagtail support
Ive recently add a gallery snippet but utilised StreamField instead for the images, just a listblock of structblocks that contain my image based fields. Ordering in the Sf bit then

Dino Luk

unread,
Nov 9, 2015, 3:20:47 AM11/9/15
to Wagtail support
OK, but then I lose ability to specify min and max number of elements, which is, to be honest, the only reason I implemented galleries this way. 
But on the other hand ordering is probably more important than these restrictions.

Matthew Westcott

unread,
Nov 12, 2015, 11:03:50 AM11/12/15
to wag...@googlegroups.com
Just had chance to dig into this a bit - sorry for the delay.

Scot: I notice that your StandardPageRelatedSnippet class is inheriting from Snippet as well as pointing to Snippet via a foreign key - is that intentional? Normally inheriting would be used for a one-to-many setup (where Snippet is an abstract class that defines a reusable set of fields, and StandardPageRelatedSnippet is a specific variant of that model that appears as an inline object on StandardPage, rather than some other page type), whereas the foreign key gives a many-to-many relation (one snippet can appear on any number of StandardPages).

I tried Dino's example code <https://groups.google.com/d/msg/wagtail/Ktk4RlnjWFM/4noF-TWsAgAJ>, but wasn't able to reproduce the issue. I brought up a fresh copy of wagtaildemo using https://github.com/torchbox/vagrant-wagtail-develop, and added a 'galleries' app with https://gist.github.com/gasman/57c363fa6f507f5be152 as the models.py file. After running ./manage.py makemigrations / migrate, I add a new gallery snippet (Snippets -> Gallery snippets -> Add gallery snippet), add three items to it, and reorder the last one so it's at the top. When I save the snippet and then return to the edit page, the items show in the updated order, as expected - also, if I change the order and resave, the new ordering is still remembered. Are you able to give specific steps to reproduce this?

Cheers,
- Matt

Dino Luk

unread,
Nov 13, 2015, 8:49:26 AM11/13/15
to Wagtail support
I found it. It was stupid mistake. Sorry for taking your time. 
Now, I really don't know why I put it there but it was there and it was causing the error. 

class GallerySnippetGalleryItems(Orderable, GalleryItem):
    item
= ParentalKey(

       
'galleries.GallerySnippet', related_name='related_gallery_items')

   
class Meta:
        verbose_name
= _('Gallery item')
        verbose_name_plural
= _('Gallery items')

This way "ordering" defined in Orderable class was not applied. To fix it, either don't define class Meta or put "ordering = ['sort_order']" in newly defined class Meta.

Matthew Westcott

unread,
Nov 13, 2015, 9:28:10 AM11/13/15
to wag...@googlegroups.com
Glad you worked it out! I'm sure you won't be the only person caught out by that, so now we know to watch out for this in future...

Ideally we'd look for a way to modify Wagtail's behaviour to prevent this from occurring, but it seems this is a general Django gotcha, rather than a Wagtail-specific one - https://docs.djangoproject.com/en/1.8/topics/db/models/#meta-inheritance. Incidentally, that documentation shows that there's another way to fix this:

class Meta(Orderable.Meta):

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at http://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/b273e807-a262-4a85-8924-62bad97f6e9a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Giovanni Carlo Marasco

unread,
Jan 22, 2017, 3:44:05 AM1/22/17
to Wagtail support
Hey Scott, I'm reviving this thread as I'm running into exactly the problem you had, I'm on Wagtail 1.8 and have followed the example from the documentation here.
Did you ever find out what was causing your issue?

Many thanks

Scot Hacker

unread,
Jan 23, 2017, 1:17:32 PM1/23/17
to Wagtail support


On Sunday, January 22, 2017 at 12:44:05 AM UTC-8, Giovanni Carlo Marasco wrote:
Hey Scott, I'm reviving this thread as I'm running into exactly the problem you had, I'm on Wagtail 1.8 and have followed the example from the documentation here.
Did you ever find out what was causing your issue?

That was a long time ago and it came up on a demo site I no longer have access to, but I seem to remember that Dino's solution above was the ticket:

This way "ordering" defined in Orderable class was not applied. To fix it, either don't define class Meta or put "ordering = ['sort_order']" in newly defined class Meta.

I do know that ordering is working perfectly as advertised at many points in my production code.

./s
 
Reply all
Reply to author
Forward
0 new messages