On Fri, Jun 19, 2009 at 12:25 PM, Facundo Casco <fca...@gmail.com> wrote:

>
> Hi, I have a problem with a query and I think it might be a bug.
> Lets say I want to get some Entries that have authors from a City.
> I use a filter like with authors__city__name='Someville'
> The problem is that I get one Entry for every Author that lives in
> 'Someville' and when one Entry has more than one Author meeting the
> condition I get duplicates for the same Entry.
>
> Can somebody please confirm I'm not doing something wrong. I'm on v1.0
> final btw.
>
> Thanks a lot.
>
>
> Below is some example code.
>
> Models::
>
>       from django.db import models
>
>       class Blog(models.Model):
>          name = models.CharField(max_length=100)
>          tagline = models.TextField()
>
>       def __unicode__(self):
>          return self.name
>
>       class City(models.Model):
>          name = models.CharField(max_length=50)
>
>       def __unicode__(self):
>          return self.name
>
>       class Author(models.Model):
>          name = models.CharField(max_length=50)
>          email = models.EmailField()
>          city = models.ForeignKey(City)
>
>       def __unicode__(self):
>          return self.name
>
>       class Entry(models.Model):
>          blog = models.ForeignKey(Blog)
>          headline = models.CharField(max_length=255)
>          body_text = models.TextField()
>          pub_date = models.DateTimeField()
>          authors = models.ManyToManyField(Author)
>
>       def __unicode__(self):
>          return self.headline
>
>
> Example code::
>
>       In [1]: from narf import pru
>
>       In [2]: b = pru.models.Blog(name='B', tagline='bb')
>
>       In [3]: b.save()
>
>       In [4]: ush = pru.models.City(name='Ushuaia')
>
>       In [5]: fte = pru.models.City(name='Calafate')
>
>       In [6]: ush.save()
>
>       In [7]: fte.save()
>
>       In [8]: jo = pru.models.Author(name='Jo',
> email='j...@example.com', city=ush)
>
>       In [9]: jo.save()
>
>       In [10]: bob = pru.models.Author(name='Bob',
> email='b...@example.com', city=ush)
>
>       In [11]: bob.save()
>
>       In [12]: e1 = pru.models.Entry(blog=b, headline='1', body_text='11',
> pub_date='2009-09-09')
>
>       In [13]: e1.save()
>
>       In [14]: e1.authors.add(jo)
>
>       In [15]: e1.save()
>
>       In [16]: e2 = pru.models.Entry(blog=b, headline='2', body_text='22',
> pub_date='2009-09-09')
>
>       In [17]: e2.save()
>
>       In [18]: e2.authors.add(jo)
>
>       In [19]: e2.authors.add(bob)
>
>       In [20]: e2.save()
>
>       In [22]:
> pru.models.Entry.objects.filter(authors__city__name='Ushuaia')
>       Out[22]: [<Entry: Entry object>, <Entry: Entry object>, <Entry:
> Entry object>]
>
> >
>
Nope, you aren't doing anything wrong, that's just how SQL works at that
level.  If you want only only one of each item you can use the distinct()
method on a QuerySet.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

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