-
Notifications
You must be signed in to change notification settings - Fork 2
Description
It is possible to for the same sortable elements to appear in two different inline formsets, with different query sets. This is explicitly supported in django-sort-order-field. The user can sort elements in either of these formsets, and the system will try its best to update order fields so items appear in the correct order relative to each other. That is, if Formset 1 has a list of all elements:
[0] FooA
[1] FooB
[2] FooC
[3] FooD
[4] FooE
[5] FooF
… and Formset 2 has a subset:
[2] FooC
[3] FooD
[5] FooF
… the sort indexes in the subset will not deviate from their initial values. This way, a change from one formset won't upset the other. So, if FooF is moved before FooD from Formset 2, the order fields for these items will be flipped (that is, borrowed from a pool of existing indexes) instead of all the forms being renumbered from 0. The end result will be…
Formset 1:
[0] FooA
[1] FooB
[2] FooC
[3] FooF
[4] FooE
[5] FooD
Formset 2:
[2] FooC
[3] FooF
[5] FooD
If an item is added to the end of either formset, it is given a sort index greater than the last item in the list. However, there is no way to reclaim an index from the beginning or middle of the list. If FooC is deleted, no other element will be assigned a sort index of 2.
(With that said, there is also a corner case where we can end up with duplicate sort indexes. If we add some items from that formset which represents a subset, those could easily duplicate the indexes in formset 1. This is fine, but a similar kind of ugly).
Ideally, it should be possible to specify the parent model for an ordering relation. That is, "these elements are ordered as children of an instance of this model". From there, we can provide a mechanism to clean up sort indexes.