django & sql - how can I efficiently store and upd

2019-08-09 08:26发布

问题:

So I have a Django app and one of the tables is basically a list of items. The user may choose to rearrange the order of this list. When they do this, I want to preserve this information so that the next time they access the app the sort order appears as they specified.

My first instinct was to just associate an ordering number with each item in the list. Then, if the user moves an item from position 5 to position 2, I would update the order number associated with that record.

I immediately realized, however, that this would mean updating every other record in the database and adjusting their order numbers. This seems inefficient.

Is there a better way to preserve and update arbitrary ordering information?

回答1:

I have answered a related question in programmers.stackexchange.com. Here is the link - https://softwareengineering.stackexchange.com/a/206699/35849

Additional Note

Instead of Int, you can use Double too. Though, there is a chance of precision error. But, I think it won't hurt :)