django& sql - 如何有效地存储和更新与我的数据库表中的记录相关联的排序顺序信息? [英] django & sql - how can I efficiently store and update sort order information associated with records in my database table?
问题描述
所以我有一个Django应用程序,其中一个表基本上是一个项目列表。用户可以选择重新排列该列表的顺序。当他们这样做,我想保留这些信息,以便下次他们访问应用程序排序顺序出现他们指定。
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.
我的第一直觉是,列表中每个项目的订购号。然后,如果用户将项目从位置5移动到位置2,我将更新与该记录相关联的订单号。
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?
推荐答案
我已经回答了一个相关的问题在programmers.stackexchange.com。以下是链接 - http://programmers.stackexchange.com/a/206699/35849
I have answered a related question in programmers.stackexchange.com. Here is the link - http://programmers.stackexchange.com/a/206699/35849
其他注意
而不是 Int
,你也可以使用 Double
。虽然,有一个精度错误的机会。但是,我认为它不会伤害)。
Instead of Int
, you can use Double
too. Though, there is a chance of precision error. But, I think it won't hurt :)
这篇关于django& sql - 如何有效地存储和更新与我的数据库表中的记录相关联的排序顺序信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!