lifeisgood wrote:
> If I have a models file
>
> class City(models.Model):
> # ...
>
> class Person(models.Model):
> # ...
> hometown = models.ForeignKey(City)
>
> class Book(meta.Model):
> # ...
> author = models.ForeignKey(Person)
>
>
> then select_related will allow me to easily find all persons and citys
> related to a given book (ie from leaf to root) but how do I retrieve a
> city object and find out which
> books are in that city??
I haven't checked it, but
models.Book.objects.filter(person__city__id__exact=city.id)
should do it, though it's not exactly what you asked for. Your way would be
reduce(QuerySet.__or__,
[person.book_set.all() for person in city.person_set.all()])
or (with patch from ticket #1807)
QuerySet.list__union(models.Book,
[person.book_set.all() for person in city.person_set.all()])
But I'd prefer the first option.
Michael
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" 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-developers
-~----------~----~----~----~------~----~------~--~---