On Thu, 2009-04-30 at 19:26 -0700, jeff wrote:
> This seems like it should be easy, but it's got me stumped.
>
> Here's a model from the Django examples:
>
> 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)
> n_comments = models.IntegerField()
> n_pingbacks = models.IntegerField()
> rating = models.IntegerField()
>
> Is there a single query to get all distinct authors for all entries in
> a certain blog? All the examples demonstrate how to get all authors
> for a single entry.
Assuming "b" is the blog object you're trying to match, you can do this:
Authors.objects.filter(entry__blog=b)
I'm not completely sure how to explain how to arrive at that solution,
I'm afraid. Presumably you had seen something that looked like
Author.objects.filter(entry=e) for some particular entry "e". This
version is the same thing, but with one extra traversal of a relation
between models. If you knew the blog name, "n", and didn't happen to
have the Blog object, "b", you could write:
Author.objects.filter(entry__blog__name=n)
Every field connection adds another double-underscore piece to the
filter.
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---