On Sun, Jul 13, 2008 at 1:37 PM, Chris <[EMAIL PROTECTED]> wrote: > > I'm reasonably fluent in SQL, and while I've found Django's ORM is > great for basic CRUD operations, I'm having trouble creating joins. > > For example, I have a three tables, Article, Rating, and User. Rating > stores a user's rating of a particular article. I'm trying to create a > view that lists articles, and optionally shows the current user's > rating of each article, if they've made one. To query the rating > record along with the article in SQL, this would just be a simple left > outer join. How would this be done with Django's ORM? I've reviewed > http://www.djangoproject.com/documentation/model-api/#relationships > but I don't think it covers this case.
Option 1 - use raw SQL. This is always an option - Django provides a nice ORM on top of basic SQL, but there are some queries that are easier to express in raw SQL than it is to mangle into the ORM. Option 2 - The Django ORM automatically creates joins as they are required by the query that is posed. For the most part, this means inner joins, but the trunk version of Django does provide some ability to customize the type of join that is used. However, this isn't currently documented; it would mean a bit of digging into the internals. Option 3 - Look into select_related(). The purpose of select_related is to populate related objects when retrieving a base object. It sounds like this might be what you are trying to do. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

