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

血红的双手。 提交于 2019-12-11 13:56:59

问题


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



来源:https://stackoverflow.com/questions/20061276/django-sql-how-can-i-efficiently-store-and-update-sort-order-information-ass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!