I guess I saw it as operating on a group of objects: filtering the
group of authors related to my_book by is_living. I also think I got
RelatedManager confused with Manager.

I think I can handle two db hits, although it's not optimal. I wish
there was a way to get a set of data filtered on many levels (ie, only
books with living authors AND only the related living authors) with
one command/db hit.

On Aug 26, 2:10 am, Eric Abrahamsen <[EMAIL PROTECTED]> wrote:
> On Aug 25, 2008, at 3:11 PM,MrJogowrote:
>
>
>
>
>
> > How do I create a custom manager for many-to-many traversal? An
> > example will illustrate what I want to do better. Suppose I have the
> > following models.py:
>
> > class Book(models.Model):
> >  title = models.CharField(max_length=100)
>
> > class Author(models.Model):
> >  books = models.ForeignKey(Book)
> >  name = models.CharField(max_length=100)
> >  is_alive = models.BooleanField()
>
> > This is a many-to-many relationship: a book can have multiple authors
> > and an author can have written multiple books.
>
> > If I have a book object my_book, and I want to get all the authors, I
> > do my_book.author_set.all(). How can I set up a custom manager to only
> > get living authors of that book, so I could do something like
> > my_book.livingauthor_set.all()?
>
> Custom managers are usually used for table-level functionality, ie  
> you'd make a manager method that filters or acts on a *group* of  
> books, not one single book. The usual thing to do when you want  
> something from a single object is to put a custom method on Book,  
> which returns living authors, like:
>
> def living_authors(self):
>      return self.author_set.filter(is_alive=True)
>
> What I don't know is if there's any way to keep this from making  
> another db hit every time you call some_book.living_authors().  
> QuerySets can't be further filtered once they've been evaluated, so  
> using select_related() on the original Book QuerySet might not help...
>
> Yours,
> Eric
>
>
>
> > Thanks
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to