问题
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