Jak wykonać filtr w modelu Django za pomocą słownika zamiast argumentów metody? To co mam tu:Model filtru Django według słownika
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 Pytanie, czy istnieje lepszy sposób ustawić wartości słownikowych niż to, co robię powyżej? Taki jak trójskładnik, ale w taki sposób, że wartość nie istnieje, jeśli nie ma?
sort_params['sort'] = sort if not None else ''
http://stackoverflow.com/questions/15949816/ use-string-variable-kwargs-as-named-argument –