django-admin-sortable2 icon indicating copy to clipboard operation
django-admin-sortable2 copied to clipboard

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

Open ldeluigi opened this issue 1 year ago • 1 comments

After some debugging I found the source of the issue: https://github.com/jrief/django-admin-sortable2/blob/3cbfa1a0a08078a6ec4dfc0e7ec67bfb9d02aeb0/adminsortable2/admin.py#L489-L491

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.

ldeluigi avatar Aug 01 '24 09:08 ldeluigi

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?

nicolochiellini avatar Sep 05 '24 15:09 nicolochiellini

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.

jrief avatar Nov 14 '24 14:11 jrief

@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.

jrief avatar Nov 15 '24 09:11 jrief

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

ldeluigi avatar Nov 15 '24 09:11 ldeluigi

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

ldeluigi avatar Nov 15 '24 09:11 ldeluigi

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

ldeluigi avatar Nov 15 '24 09:11 ldeluigi

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

ldeluigi avatar Nov 15 '24 09:11 ldeluigi

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

jrief avatar Nov 15 '24 10:11 jrief

Hi! I just stumbled upon a problem that when adding a new object with new inlines, every inline model is saved with order = 0 (where 0 is the default value for the field, as suggested by the docs).

I think the condition should be changed to if order_field_value is None or order_field_value <= 0:. This way ordering is preserved if specified (when > 0), and auto-ordered when new objects are created.

EDIT: should I create a new PR to fix this?

zlorf avatar Jan 31 '25 15:01 zlorf

It would break when you update a list I think

ldeluigi avatar Jan 31 '25 17:01 ldeluigi

The ideal solution would be to change the javascript so that new inlines have order field equal to a negative number, this way they can be distinguished from the first in order on update

ldeluigi avatar Jan 31 '25 17:01 ldeluigi

  1. I've tested it in my new project with the change I mentioned and it works correctly for both new and updated lists. However, I haven't tried "save as new" yet, just adding a new model with inlines and editing an existing one.
  2. IIUC, for editing a list order, JavaScript sets the numbers for order and it starts with 1. So there is a clear distinction between new elements (0) and edits (>0).

zlorf avatar Jan 31 '25 17:01 zlorf

I've just tried "save as new" and the order is preserved as it should.

I'm sending a PR in that case.

zlorf avatar Jan 31 '25 17:01 zlorf

https://github.com/jrief/django-admin-sortable2/pull/416 sent.

zlorf avatar Jan 31 '25 17:01 zlorf

Also experiencing the same issue, looking forward to the PR being merged!

stefanofusai avatar Feb 08 '25 15:02 stefanofusai