> 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 -~----------~----~----~----~------~----~------~--~---