用户过滤的搜索在django [英] User-filtered search in django

查看:120
本文介绍了用户过滤的搜索在django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为类似于iTunes Smart Playlists的用户集思广益的搜索功能。我现在可以显示数据库中所有电影的所有发行年份。用户可以选一年,看看那一年发行的所有电影。或者我可以显示所有的电影流派。用户可以选择类型,并查看符合该条件的所有电影。

I'm brainstorming a search function for the user that is similar to iTunes Smart Playlists. I can currently display all of the release years for all of the films in the database. A user can pick a year and see all of the films released in that year. Or I can show all of the genres of movie. A user can choose a genre and see all of the movies that match that criteria.

我想要的是用户可以动态选择自己的标准的表单。所以例如发布日期在2000之后,类型不是恐怖将返回过滤的列表。所有这些选择都将在用户提供的表单中具有下拉列表选项。

What I want is a form that the user can dynamically choose their own criteria. So for instance "Release Date" "is after" "2000" AND "Genre" "is not" "Horror" would return a filtered list. All of these choices would be user supplied within a form that has dropdown choices.

从SQL的角度来看,这似乎很简单,但我想知道最好的实现将从django形式转到MySQL。任何关于起点的建议?

It seems simple from a SQL point of view, but I'm wondering what the best implementation would be from to go from a django form to MySQL. Any suggestions on a starting point?

推荐答案

从您的表单元素构建字典。

Build dictionaries from your form elements.

incdict = {'releasedate__gte': datetime.datetime(2000, 1, 1)}
excdict = {'genre': 'Horror'}
moviequery = Movie.objects.filter(**incdict).exclude(**excdict)

这篇关于用户过滤的搜索在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆