Tim and Nis, Okay thanks for the help. My view is definitily better now than before. Here is my new view
def searchresult(request): if request.method == 'POST': myset = set() NOT_PICKED = "---------------" y = Choice.objects.all() if ('price' in request.POST and request.POST['price'] <> NOT_PICKED): y = y.filter(price__price_cat=request['price']) if ('size' in request.POST and request.POST['size'] <> NOT_PICKED): y = y.filter(size__size_cat=request['size']) for q in y: styles = Choice.objects.get(id=q.id).style_set.all() if ('color' in request.POST) and (request.POST['color'] <> NOT_PICKED): styles = styles.filter(color_cat=request['color']) for style in styles: myset.add(style) if myset == set([]): return render_to_response('searchresult_none.html', {'s': "No product available"}) else: return render_to_response('searchresult.html', {'s': myset}) //////////////////// Does that look any better? I have two issues with this. 1) Is there anyway to get it so that I don't have to use the following for loop: for style in styles: myset.add(style) 2) Is still adds duplicate products. If a product contains two choices that have a price of 149 and 199. Then when a user searches by price only (100-199) then the product is displayed twice in the result set. //////// Thank you SO much for your help!!! This is my first time developing in Python and I'm learning quite a bit. On Aug 9, 8:37 am, Tim Chase <[EMAIL PROTECTED]> wrote: > > NO_COLOR = "---------------" > > styles = Choice.objects.get(id=h.id).style_set.all() > > if ('color' in request.POST) and (request.POST['color'] <> NO_COLOR): > > styles = styles.filter(color_cat=request['color']) > > for u in styles: > > dict[u] = u > > > //// > > > I did notice that whenever I do a search for just the color 'brown'. > > Then the result set will bring back the same style however many > > different sizes that are in the style. So if an area rug sells two > > choices (2'x3' 39.00, 4'x6' 149.00). Then that style will show up > > twice in the result set. Is there anyway better to filter out the > > styles that have already been added to the dictionary. My previous > > code worked..but not sure it's the best way to do it. Here it is: > > > num = 0 > > for a in dict: > > if a == j: # 'j' being the name of the style and 'a' is the name of > > the style that is already in the dictionary > > num = 1 > > if num == 0: > > dict[j] = j > > I second the suggestion by Nis to make meaningful variable names, > as well as the suggestion to use sets rather than abusing a > dictionary (and tromping on the namespace with "dict"...just got > bitten by this yesterday, a "zip"-code variable shadowed the > built-in zip() command causing some confusing errors) > > It looks like you could just do something like > > results = Choice.objects.get(id=h.id).style_set.all() > # filter results > results = set(results) > > I'm not sure on the performance of set creation, so you might > compare the results with > > results = set(list(results)) > > or > > results = set(tuple(results)) > > -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 -~----------~----~----~----~------~----~------~--~---