【发布时间】:2019-10-05 02:12:27
【问题描述】:
我正在寻求关于如何检测用户正在使用哪些过滤器的建议,过滤系统可以有不同的选项来获取数据,但是使用 if 语句来检查值是否来自 POST 然后添加将它添加到一个过滤器集并不是一个很好的选择,特别是当它们很多时。
# Some if statements detecting if a filter is used (if it is not null in the POST)
# Adding the filter to filters
filters = {
# filters after being added
'{}__{}'.format('categories', 'exact'): request.POST['category'],
'{}__{}'.format('price', 'gte'): request.POST['price'], # Only an example
}
products = Product.objects.filter(**filters)
这行得通,但我只想知道你会推荐什么。
【问题讨论】: