django-admin-sortable2
django-admin-sortable2 copied to clipboard
Selected ordering is not preserved when using "Save as new"
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.
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.
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?
It would break when you update a list I think
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
- 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.
- 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).
I've just tried "save as new" and the order is preserved as it should.
I'm sending a PR in that case.
https://github.com/jrief/django-admin-sortable2/pull/416 sent.
Also experiencing the same issue, looking forward to the PR being merged!