Django:从 GET 请求生成查询集
我有一个使用 GET 方法的 Django 表单设置。每个值对应于 Django 模型的属性。生成查询的最优雅的方式是什么?目前,这就是我在视图中所做的事情:
def search_items(request):
if 'search_name' in request.GET:
query_attributes = {}
query_attributes['color'] = request.GET.get('color', '')
if not query_attributes['color']: del query_attributes['color']
query_attributes['shape'] = request.GET.get('shape', '')
if not query_attributes['shape']: del query_attributes['shape']
items = Items.objects.filter(**query_attributes)
但我很确定有更好的方法来解决它。
I have a Django form setup using GET method. Each value corresponds to attributes of a Django model. What would be the most elegant way to generate the query? Currently this is what I do in the view:
def search_items(request):
if 'search_name' in request.GET:
query_attributes = {}
query_attributes['color'] = request.GET.get('color', '')
if not query_attributes['color']: del query_attributes['color']
query_attributes['shape'] = request.GET.get('shape', '')
if not query_attributes['shape']: del query_attributes['shape']
items = Items.objects.filter(**query_attributes)
But I'm pretty sure there's a better way to go about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用列表比较和“感兴趣的参数”集来完成此操作:
只是为了好玩(又名实际上并不这样做),您可以在一行中完成:
You could do it with a list comp and and "interested params" set:
Just for fun (aka don't actually do this) you could do it in one line:
好吧,你处理这个问题的基本方式似乎是合理的,但你写出来的方式看起来有点有趣。我可能会这样做:
well, the basic way you are approaching the problem seems sound, but the way you wrote it out looks a little funny. I'd probably do it this way:
如果您希望它完全动态,您可以使用一点模型自省来找出您实际可以查询的字段,并仅使用这些字段进行过滤。
不过,这个解决方案不允许您在 GET 参数中使用 __lookups,不知道您是否需要它。
If you want it to be fully dynamic, you can use a little bit of model introspection to find out what fields you can actually query, and filter only using those.
Though, this solution won't allow you to use __lookups in GET parameters, don't know if you need it.
假设“颜色”和“形状”是必需的 GET 参数。出于安全原因,首选过滤参数的预定义元组。
Suppose 'color' and 'shape' are required GET params. Predefined tuple of filtering params is prefered because of security reasons.