[英]Django: most efficient way to query many records?
I have a table with a few thousands records (products). 我有一个带有数千个记录(产品)的表。 Each product has a 4 different categories: 每个产品都有4个不同的类别:
CAT1 CAT2 CAT3 CAT4
I wonder if is there a method, or what is the best practice, to dynamically retrive the available categories based on the categories already selected (using Ajax). 我想知道是否有一种方法或最佳实践是根据已经选择的类别(使用Ajax)动态地检索可用的类别。 Example: 例:
if CAT1 = green
all the products with CAT1 = green
will have a series of CAT2
categories and so on. 如果CAT1 = green
所有产品CAT1 = green
将有一系列的CAT2
类等。 I would like to know which are the CAT2 CAT3 CAT4 categories whose products match CAT1 = green
. 我想知道哪些CAT2 CAT3 CAT4类别的产品与CAT1 = green
匹配。 Once I set a value or CAT2
as well I would like to do the same based on CAT1 && CAT2
values. 设置值或CAT2
,我也想根据CAT1 && CAT2
值进行相同的操作。
Thanks. 谢谢。
This is a technique commonly known as "select chaining" or "chained selects". 这是一种通常称为“选择链接”或“链接选择”的技术。
You can use some fairly simple javascript for this as shown in the answer to How to limit choice field options based on another choice field in django admin 您可以为此使用一些相当简单的javascript,如django admin中如何基于另一个选择字段来限制选择字段选项的答案所示。
You can also use a prepackaged solution such as django-smart-selects (found via the SO answer to django chain select ) 您还可以使用预打包的解决方案,例如django-smart-selects (通过django chain select的SO答案找到)
Thanks for the replies. 感谢您的答复。 The Chained select only works partially as I don't have a hierarchical structure. 链式选择仅部分起作用,因为我没有层次结构。 Here is an example of data: 这是数据示例:
PRODUCT 1
CAT1 = vegetables
CAT2 = heavy
CAT3 = green
PRODUCT 2
CAT1 = vegetable
CAT2 = light
CAT3 = red
PRODUCT 3
CAT1 = diary
CAT2 = heavy
CAT3 = red
In my template I would like to make a system so that when the users choose CAT1 = vegetables
they see CAT2
options to be heavvy and light while if they choose CAT1 = diary
the only option for CAT2
is heavy and so on. 在我的模板中,我想建立一个系统,以便当用户选择CAT1 = vegetables
他们看到CAT2
选项繁重而轻便,而如果他们选择CAT1 = diary
,则CAT2
的唯一选项是沉重的,依此类推。
My way to go would be to get a json of the entire product table and look for the available values once a category has been selected... but the products table comprises thousands of items and Im quite sure it will slow down the entire app. 我的方法是获取整个产品表的json并在选择类别后查找可用值...但是products表包含数千个项目,我非常确定这会降低整个应用的速度。 There is something similar in the admin page when you add the Filter function ( list_filter
) as it shows only the fields that have some entries in them. 添加过滤器功能( list_filter
)时,管理页面中会有类似的内容,因为它仅显示其中包含某些条目的字段。
THANKS! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.