User-filtered search in django

强颜欢笑 提交于 2020-01-06 07:15:26

问题


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.

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.

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?


回答1:


Build dictionaries from your form elements.

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


来源:https://stackoverflow.com/questions/4161371/user-filtered-search-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!