简体   繁体   中英

django & sql - how can I efficiently store and update sort order information associated with records in my database table?

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM