Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » How to use a Queryset from one model to filter the Queryset from another model [RSS Feed]

#1 March 25, 2008 01:23:42

d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to use a Queryset from one model to filter the Queryset from another model


This seems like something that should be doable, but I can't seem to
make it work.

Consider an example with these models:

class Book(models.Model):
author = models.ForeignKey(Author)
publisher = models.ForeignKey(Publisher)
pub_date = models.DateTimeField()
is_hardcover = models.BooleanField()
pages = models.IntegerField()

class Author(models.Model):
name = models.CharField(...)

class Publisher (models.Model):
name = models.CharField(...)

I want to get a list of all of the books, authors and publishers that
were published in 2005 in hardcover with over 300 pages. So, this is
an easy queryset to create:

recent_books = Book.objects.filter(pub_date__year='2005',
is_hardcover= True, pages>300)

and I know that I can get them this way:
recent_authors =
Author.objects.filter(book__pub_date__year='2005',is_hardcover= True,
pages>300) .distinct()
recent_publisher =
Publisher.objects.filter( book__pub_date__year='2005',is_hardcover=
True, pages>300).distinct()

but that isn't very DRY.

I would like to be able to do something like:
recent_authors = Author.objects.filter(book in recent_books)

Can an equivalent be done? How would you do it? Or alternatively, how
would you keep this code DRY?

Thanks in advance,
Dave
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 25, 2008 02:18:54

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to use a Queryset from one model to filter the Queryset from another model


How about recent_authors = Author.object.filter(book__pk__in=).distinct()

That can probably be done without the list comprehension but this
should work.

On Mar 24, 7:23 pm, davenaff <> wrote:
> This seems like something that should be doable, but I can't seem to
> make it work.
>
> Consider an example with these models:
>
> class Book(models.Model):
> author = models.ForeignKey(Author)
> publisher = models.ForeignKey(Publisher)
> pub_date = models.DateTimeField()
> is_hardcover = models.BooleanField()
> pages = models.IntegerField()
>
> class Author(models.Model):
> name = models.CharField(...)
>
> class Publisher (models.Model):
> name = models.CharField(...)
>
> I want to get a list of all of the books, authors and publishers that
> were published in 2005 in hardcover with over 300 pages. So, this is
> an easy queryset to create:
>
> recent_books = Book.objects.filter(pub_date__year='2005',
> is_hardcover= True, pages>300)
>
> and I know that I can get them this way:
> recent_authors =
> Author.objects.filter(book__pub_date__year='2005',is_hardcover= True,
> pages>300) .distinct()
> recent_publisher =
> Publisher.objects.filter( book__pub_date__year='2005',is_hardcover=
> True, pages>300).distinct()
>
> but that isn't very DRY.
>
> I would like to be able to do something like:
> recent_authors = Author.objects.filter(book in recent_books)
>
> Can an equivalent be done? How would you do it? Or alternatively, how
> would you keep this code DRY?
>
> Thanks in advance,
> Dave
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 25, 2008 16:47:00

d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to use a Queryset from one model to filter the Queryset from another model


Alex,

Good thought. Not as crisp as I would like, but it should do the
trick.

Thanks,
Dave

On Mar 24, 6:18 pm, "" <>
wrote:
> How about recent_authors = Author.object.filter(book__pk__in=).distinct()
>
> That can probably be done without the list comprehension but this
> should work.
>
> On Mar 24, 7:23 pm, davenaff <> wrote:
>
> > This seems like something that should be doable, but I can't seem to
> > make it work.
>
> > Consider an example with these models:
>
> > class Book(models.Model):
> > author = models.ForeignKey(Author)
> > publisher = models.ForeignKey(Publisher)
> > pub_date = models.DateTimeField()
> > is_hardcover = models.BooleanField()
> > pages = models.IntegerField()
>
> > class Author(models.Model):
> > name = models.CharField(...)
>
> > class Publisher (models.Model):
> > name = models.CharField(...)
>
> > I want to get a list of all of the books, authors and publishers that
> > were published in 2005 in hardcover with over 300 pages. So, this is
> > an easy queryset to create:
>
> > recent_books = Book.objects.filter(pub_date__year='2005',
> > is_hardcover= True, pages>300)
>
> > and I know that I can get them this way:
> > recent_authors =
> > Author.objects.filter(book__pub_date__year='2005',is_hardcover= True,
> > pages>300) .distinct()
> > recent_publisher =
> > Publisher.objects.filter( book__pub_date__year='2005',is_hardcover=
> > True, pages>300).distinct()
>
> > but that isn't very DRY.
>
> > I would like to be able to do something like:
> > recent_authors = Author.objects.filter(book in recent_books)
>
> > Can an equivalent be done? How would you do it? Or alternatively, how
> > would you keep this code DRY?
>
> > Thanks in advance,
> > Dave
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » How to use a Queryset from one model to filter the Queryset from another model [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support