[英]Pass kwargs into Django Filter
When viewing model entries from within Django Admin, you can specify filters. 从Django Admin中查看模型条目时,您可以指定过滤器。 How can I mimic this behavior?
我怎么能模仿这种行为? Not to familiar with
kwargs
but something similar to this: 不熟悉
kwargs
但有类似的东西:
foo = Model.objects.filter(**__exact='**')
where the first set of **
would be a field in the model and the second set would be an entry. 第一组
**
将是模型中的一个字段,第二组将是一个条目。 Basically making the queries variable, based on what the user chooses on the front end. 基本上根据用户在前端选择的内容使查询变量。 How would I send that variable sort option to the view, and then return it back to the webpage.
如何将该变量排序选项发送到视图,然后将其返回到网页。 What about using a dictionary?
怎么用字典? Please help
请帮忙
This SO question has proven to be a little helpful, but still cannot grasp it completely. 这个问题已被证明有点帮助,但仍然无法完全掌握。
You can unpack a python dict as your filter parameters using **
您可以使用
**
解压缩python dict作为过滤器参数
your_filters = {
'field_1__exact': value_1,
'field_2__gte': value_2,
}
Model.objects.filter(**your_filters)
Said that, you can built your query filters(a python dict) dynamically based on an user input. 说,你可以根据用户输入动态构建你的查询过滤器(python dict)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.