您可以使用kwarg格式直接对其进行迭代(我不知道合适的术语)
argument_list = [] #keep this blank, just decalring it for laterfields = ('title') #any fields in your model you'd like to search againstquery_string = 'Foo Bar' #search terms, you'll probably populate this from some sourcefor query in query_string.split(' '): #breaks query_string into 'Foo' and 'Bar' for field in fields: argument_list.append( Q(**{field+'__icontains':query_object} ) )query = Entry.objects.filter( reduce(operator.or_, argument_list) )# --UPDATe-- here's an args example for completenessorder = ['publish_date','title'] #create a list, possibly from GET or POST dataordered_query = query.order_by(*orders()) # Yay, you're ordered now!
这将在您
query_string的每个字段中查找每个字符串,
fields然后对结果进行或运算
我希望我仍然有这个原始来源,但这是根据我使用的代码改编的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)