jrief/django-admin-sortable2

Selected ordering is not preserved when using "Save as new"

Closed this issue · 8 comments

After some debugging I found the source of the issue:

if order_field_value is None or order_field_value >= 0:
max_order = self.get_max_order()
setattr(obj, self.default_order_field, max_order + 1)

Basically when saving as new the order passed with the form submission gets discarded because its value is greater than or equal to 0, as per order_field_value >= 0.

Instead, when saving as new after a customization of the field ordering, the selected order should be honored.

Hello, i came across the same bug while "saving as new" an instance with sorted inlines.
The inlines of the new instance are not sortable.
I've tried to fix it with "reoder" management command but nothing happen, still unsortable.
I've tried to reoder only the inlines but nothing: index are good but still unsortable.

for order, obj in enumerate(articolo_obj.elementoarticolo_set.iterator(), start=1):
    setattr(obj, 'indice', order)
    obj.save()

Somebody has an idea how to do a quick fix while waiting for a patch?

Thanks @ldeluigi for this pull request. Please be patient, I need some time to test this. I am just wondering, if this might cause some regressions, otherwise that comparison wouldn't be the way it is in the first place.

@nicolochiellini @ldeluigi I just tested this in my local environment. In my setups the value of order_field_value is always None and therefore it always works. How is it possible that in your use-case that value is negative?

Anyway, I'm going to merge this, although I have some fears that it might cause a regression.

I have no idea, I tested with the testapp in this repository!

PS. The value is not negative, I just needed the if condition to not pass if it is 0 or greater

The negative check is in case someone messes with the template by overriding in some strange way that would cause negative values to appear

In other words, when saving as new, the order_field_value already contains the desired value and should not be overwritten, that's why I inverted the if condition

Let's see if anyone complains about this after upgrading to version 2.2.4.