I find the general approach (good attention to error handling, abstraction of the code into a manager method) to be really excellent here, its very nice.
It’s worth discussing a bit more the F
function in the manager method, which essentially updates a DB column of integers. Every time a single Step
gets move()
d, between 1
and N
steps get updated in the DB — an O(N)
algorithm.
For readers sure to use small N
(most applications, in reality) this is fine, don’t sweat it.
For readers whereN
increases dramatically, or where there is a lot of sorting activity, a linked list will be far more appropriate, giving an O(1)
algorithm to save hammering the DB.
HOWEVER it will also be necessary to write deletion handlers to ensure referential integrity of the list, so it’s a bit more tricky overall and worth avoiding unless you meet this specific use case.
— —
Also worth considering is the django-orderable
library at https://github.com/incuna/django-orderable
I’ve not tried it to be fair: it just looks ok, and seems to allow nice reordering in the django admin... so worth considering as an out-of-the-box solution. It uses the same/similar F
approach used above.