I want to create a model that will order its children models in the appropriate way. For instance, a Book has many Chapters, but the Chapters have to be in a specific order.
I assume that I need to put an IntegerField on the Chapter model that specifies the order of the Chapters like the following question suggests: Ordered lists in django
My main issue is that whenever I want to insert a new Chapter in between two existing chapters or reorder them in any way, I have to update (almost) every Chapter in the Book. Is there a way (perhaps in the Django Admin, which I’m using) to avoid having to manually change every index on every Chapter whenever I change the order?
I’m not a big fan of creating a “Linked List” style model, as proposed in the above-linked question, as I am under the impression that’s not good practice for database creation.
What is the “right” way to model this relationship?
The answer you alluded to was probably the best way to handle this efficiently. Probably requiring a raw SQL statement
UPDATE Chapter SET order = order + 1 WHERE book_id = <id_for_book> AND order <= <insert_index_location>. For Django 1.1+: You could useF()to write this in a single line as the following, but it might still be O(n) queries under the hood, using transactions.