Ken wrote:
> I need some advice.  I'm struggling with a query that spans 4 tables.
> "Struggling" is a bad word; I'll explain later.  Django uses a model-
> oriented query system together with a Foreign Key manager to get to
> the "next" joining table.  A query that spans several tables results
> in a deeply nested loop.  Hence, my struggle.  I have an aversion to
> writing deeply nested loops.  It's an indication that I dont quite
> understand my problem.  In SQL, you join the tables you need depending
> on the query and you filter out entries you are interested in with a
> WHERE clause.  It's an idea I'm familiar with; it's "flat", ie, not
> nested.  When I see deeply nested loops, I tend to think recursion.
> But that doesnt work either since at each level, the operation is
> model-specific.  I need some general advice on how folks handle
> queries on joined tables within the django framework.  Since queries
> are model based, is it better to forget about constructing related but
> orthogonal tables and simply throw everything in a model?
> 

I have models: Artist, Track, Album, MultiAlbum (think box set).
Artist.tracks is actually the Django created reverse mapping of 
Track.artist. Once I fully understood the foo__bar__baz notation I found 
it easier to read and create than the equivalent SQL.

The line of code below returns all of the Artists in the chosen 
multi-album where self.title is the box set's title.

Artist.objects.filter(tracks__album__multi__title=self.title).distinct()

Hope this helps. If not, perhaps you have some examples of your own to 
share.

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