[EMAIL PROTECTED] wrote:
> Thanks; this works for me -- though I have to do
> 
>      Author.objects.filter(story__id__isnull=False).distinct()
> 
> Or it repeats each author for each of his stories.
> 
> This still confuses me, because there is no "story" field in the Author
> model to specify how to do the join (Author:Story is One:Many, so I
> just put a foreign key in the Story model).  I guess it is inferring
> the Join conditions by looking at the Story model.  But what if I had
> two Story-Author relations, say:
> 
> class Story (models.Model) :
>   author = ForeignKey(Author)
>   editing_author = ForeginKey(Author)
> 
> How could it possibly know which one I wanted to join on?

When specifying more than one ForeignKeys to a model Django requires you 
to explicitly specify related names for master-to-child relations:

     class Story(models.Model):
       author = models.ForeignKey(Author, related_name='stories')
       editor = models.ForeignKey(Author, related_name='edited_stories')

Then you do:

     Author.objects.filter(edited_stories__id__isnull=False).distinct()

This related_name is also used for related managers:

     author.edited_stories.all()


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