Luke, That's brilliant, thank you so much! Just out of interest, do you know why the query was returning dupes in the first place, and why my while loop was not removing them?
Thanks again, Tom On Aug 8, 2:51 pm, Luke Seelenbinder <[email protected]> wrote: > Tom, > > make your line read: > results = Film.objects.filter( > Q(title__icontains=q) | > Q(director__name__icontains=q) | > Q(actors__name__icontains=q) | > Q(screenwriters__name__icontains=q) | > Q(genre__icontains=q) > ).distinct() > > So just add the distinct method to the Q set. And then you won't need > the while loop at all. > Read:http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct > > Hope that helps. > > Luke > > > > On Sat, Aug 8, 2009 at 9:19 AM, Thomas Scrace<[email protected]> wrote: > > > Hi all, > > > In order to teach myself Django I am creating a simple film database > > project. My search code looks like this: > > > results = Film.objects.filter( > > Q(title__icontains=q) | > > Q(director__name__icontains=q) | > > Q(actors__name__icontains=q) | > > Q(screenwriters__name__icontains=q) | > > Q(genre__icontains=q) > > ) > > > This lets me have a 'catch-all' search box that allows you to search > > on all of the fields of my film class. It works, but I always get > > duplicate results. The same film will appear several times (typically > > 3, 4 or 6 times) in the search results. > > > As a sort of kluge to get around this, and to try to further diagnose > > what was happening, I wrote the following to try to remove duplicate > > results: > > > films = list(results) > > while len(films) > 1: > > if films[len(films)-1] == films[len(films)-2]: > > films.remove(films[len(films)-1]) > > This executes without any errors, but does not remove duplicate > > results from the list, from which I can only infer that for some > > reason the results that I think are identical are in fact different in > > some way. > > > My models look like this: > > > class Director(models.Model): > > name = models.CharField(max_length=70) > > > def __unicode__(self): > > return u'%s' % (self.name,) > > > class Actor(models.Model): > > name = models.CharField(max_length=70) > > > def __unicode__(self): > > return u'%s' % (self.name,) > > > class Screenwriter(models.Model): > > name = models.CharField(max_length=70) > > > def __unicode__(self): > > return u'%s' % (self.name,) > > > class Film(models.Model): > > title = models.CharField(max_length=100) > > genre = models.CharField(max_length=40) > > actors = models.ManyToManyField(Actor) > > screenwriters = models.ManyToManyField(Screenwriter) > > director = models.ForeignKey(Director) > > release_date = models.DateField(blank=True, null=True) > > > def __unicode__(self): > > return self.title > > > Does anybody have any ideas on what is happening here? I am flummoxed!. > > > Thanks in advance, > > > Tom --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] 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 -~----------~----~----~----~------~----~------~--~---

