Come si fa un filtro su un modello Django usando un dizionario piuttosto che gli argomenti del metodo? Questo è quello che ho proprio qui:Modello di filtro Django per dizionario
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...
domanda laterale, c'è un modo migliore impostare i valori del dizionario di quello che sto facendo di cui sopra? Come un ternario, ma in un modo che renderebbe il valore non esiste se non è nessuno?
sort_params['sort'] = sort if not None else ''
http://stackoverflow.com/questions/15949816/ use-string-variable-kwargs-as-named-argument –