> I have a form that will contain 3 select input types. The user is > going to have the ability to narrow down the products by selecting a > value in these 3 select boxes (Price, Size, Color). However, the user > also has the ability to not select anything. In this case I would > want to return all the records. Is there a way in django that I can > do this? This is what I currently have for Price: > > a = Price.objects.filter(id=request['price'])
Request objects are chainable and lazy, so you can do something like a = Price.objects.all() if 'price' in request: a = a.filter(id=request['price']) this can lead to code like a = Price.objects.all() for criterion in ('price', 'color', 'size'): if criterion in request: a = a.filter(id=request[criterion]) or if the field each form-field maps to differs from "id" field_map = [ ('price', 'id'), ('color': 'field2'), ('size', 'field3'), ] for fieldname, formfieldname in field_map: if formfieldname in request: a = a.filter(**{fieldname: request[formfieldname]}) which leaves "a" filtered by price/size/color based on any of the request parameters. IMHO, a Price object is an odd partitioning of data, as it sounds more like an attribute of some other model. But given that as your data model, the above should do what you describe. -tim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---