Django: Generating a queryset from a GET request
Posted
by
Nimmy Lebby
on Stack Overflow
See other posts from Stack Overflow
or by Nimmy Lebby
Published on 2010-10-24T23:38:09Z
Indexed on
2010/12/27
20:54 UTC
Read the original article
Hit count: 455
I have a Django form setup using GET method. Each value corresponds to attributes of a Django model. What would be the most elegant way to generate the query? Currently this is what I do in the view:
def search_items(request):
if 'search_name' in request.GET:
query_attributes = {}
query_attributes['color'] = request.GET.get('color', '')
if not query_attributes['color']: del query_attributes['color']
query_attributes['shape'] = request.GET.get('shape', '')
if not query_attributes['shape']: del query_attributes['shape']
items = Items.objects.filter(**query_attributes)
But I'm pretty sure there's a better way to go about it.
© Stack Overflow or respective owner