Sunday, May 1, 2011

Re: fetching data from intermediate many-to-many table

Sorry, but your question is too general as for me. Django doesn't support SQL joins directly. Could you specify with an example what data do you need to get from database using "joins"?

For example if you need to retrieve all books with zero associated sequences (empty sequences list), then code snippet may looks something like this:

Book.objects.annotate(s_count=Count('sequences')).filter(s_count=0)

May be if you read docs on ManyToMany relations, you'll find more nicer solution.

2011/5/1 А.Р. <4d876b82@gmail.com>
2011/5/1 Oleg Lomaka <...@gmail.com>:

> bs = BookSequence.objects.filter(book__pk=1).select_related()
> for s in bs:
>        print s.sequence.name, s.number_in_sequence

Oh, thanks!
Is it possible then to do left/right outer joins, as there may exist
books without
sequences and sequences without books?

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment