Làm thế nào để làm một bộ lọc trên Mô hình Django bằng cách sử dụng một đối số từ điển chứ không phải là phương thức? Đây là những gì tôi có quyền ở đây:Mô hình Bộ lọc Django theo từ điển
class StoreView(TemplateView):
def get(self, request):
# A bunch of gets
sort = request.GET.get('sort')
sort_price = request.GET.get('sort_price')
sort_mfr_method = request.GET.get('mfr_method')
# The params tpsort by
sort_params = {}
if sort is not None:
sort_params['sort'] = sort
if sort_price is not None:
sort_params['sort_price'] = sort_price
if sort_mfr_method is not None:
sort_params['sort_mfr_method'] = sort_mfr_method
# The Model Query
design_list = models.Design.objects.filter(sort_params)
# etc...
Side Câu hỏi, là có một cách tốt hơn thiết lập các giá trị điển so với những gì tôi đang làm trên? Chẳng hạn như một thứ ba, nhưng theo một cách mà sẽ làm cho giá trị không tồn tại nếu nó không có?
sort_params['sort'] = sort if not None else ''
http://stackoverflow.com/questions/15949816/ use-string-variable-kwargs-as-named-argument –