incuna / django-orderable

Add manual sort order to Django objects via an abstract base class and admin classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve efficiency of reorder_view

maxpeterson opened this issue · comments

The reorder_view calls save on each model. Since it is dealing with a set of ids it could be more efficient to use update.

Based on #13 the algorithm could be:

  • Find the min and max of the items being reordered.
  • Check that the items being reordered fit between the min and max.
  • Move items being reordered to the end of the list.
  • Update items to be consecutive numbers between min and max.

Using bulk_update would bypass Orderable.save() and therefore any "jiggery pokery"

Yeah, I don't think that update is the way to go here.

I would actually say that having a public way of permuting the order of a group of elements in a single query would be very useful, so long as the db is clever enough to know it's a permutation and therefore doesn't break uniqueness. It would make it similar to the set_RELATED_order function in django's naive order with respect to feature.

Sorry didn't mean to close!

Using DEFERRABLE might be a good solution for this if it ever becomes available.

Ref: https://code.djangoproject.com/ticket/20581

Another possibility is to use fractions as suggested here: https://begriffs.com/posts/2018-03-20-user-defined-order.html

It unfortunately recommends a postgres extension and there's some subtleties, but it might be worth the effort.

connection.constraint_checks_disabled is worth investigating.

Looks like this was addressed in #47