-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tom,

The reason it was returning dupes is simply you had multiple querysets
that matched some of the same items.
As for the while loop, it was malformed. It looks like an infinite
loop to me... I might be wrong; however, a for loop would have better
served you, e.g.,

films = []
for r in results:
    #checks if instance already films list
    #if it is, this is a dupe, do nothing
    if not r in films:
        #if not this is not a dupe, append to list
        films.append(r)


that would have served the same purpose as the distinct method.

Luke

On Sat, Aug 8, 2009 at 10:21 AM, Tom wrote:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkp9n94ACgkQXQrGVCncjPyeLQCeKGYLvv4jL2vDLLQ+DQKh/1+d
lg0AoIEABek7zEx800mmEpnjzUH0uxVa
=w/tR
-----END PGP SIGNATURE-----

>
> 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 <luke.seelenbin...@gmail.com>
> 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<t.scr...@gmail.com> 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 django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to