开发者

Django: Storing ordered, arbitrary references

开发者 https://www.devze.com 2022-12-23 12:48 出处:网络
I\'m new to Django, and I\'m not sure what I want is possible: I have a number of items that I want each AppUser (extended User model) to be able to reference.

I'm new to Django, and I'm not sure what I want is possible:

I have a number of items that I want each AppUser (extended User model) to be able to reference.

That is, g开发者_如何学编程iven an AppUser, I want to be able to extract its list of items in the way that AppUser has chosen to order them. In general, these items would actually be references to something else in the database, and this led me to one possible solution:

Store the keys for the given objects in a CommaSeparatedIntegerField in AppUser. This way, a user could have stored {7, 3, 232, 42, 1} in their items field and both the references and their preferred order would be stored. However, this feels hacky. Since most db backends store CommaSeparatedIntegerField as a VARCHAR internally, the user is not only limited by a number of objects, but also the number of digits in their chosen items. Eg. "you may store 10 items if you choose items with itemID < 10, but only 5 items if 10 < itemID < 100".

(EDIT: If this is in fact not an ugly hack, what are the performance penalties for using this solution?)

Is there a better way to do this?


Use a ManyToMany with an explicit through table containing a field giving the ordering.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号