I've got a reviews/rankings web application, a la Digg. My django application content has got the following model:

class Content(models.Model):
    title = models.CharField(max_length=128)
    url = models.URLField(max_length=2048)
    description = models.TextField(blank=True)

class Recommendation(models.Model):
    user = models.ForeignKey(User)
    content = models.ForeignKey(Content)
    review = models.TextField()
    rating = models.PositiveSmallIntegerField()
    class Meta:
        unique_together = ('user', 'content')

class Subscription(models.Model):
    subscriber = models.ForeignKey(User, related_name='subscription_set')
    publisher = models.ForeignKey(User, related_name='publication_set')
    class Meta:
        unique_together = ('subscriber', 'publisher')

I wish to create a page with the recommendations of all of the customers to whom a present user (request.user) subscribes.

Basically write this in SQL, In my opinion I'll finish track of a question like the following:

select content_content.*, content_recommendation.*, auth_user.*
from content_content, content_recommendation, content_subscription, auth_user
where content_content.id = content_recommendation.content_id
 and content_recommendation.user_id = content_subscription.publisher_id 
 and content_subscription.subscriber_id = ?
 and auth_user.id = content_subscription.publisher_id;

Wouldso would I express this using Django's query APIs? I have browse the paperwork, but simply can't get my mind around it.

I'd use:

Recommendation.objects.filter(user__publication_set__subscriber=request.user).select_related()

Which will enable you to get all of the Recommendation objects while you asked for, and also the choose_related will load all of the related User and Content objects into memory to ensure that subsequent access of these will not hit the DB again.

How you'd contruct this question really offers quite a bit related to your handling from the came back data later on though. It might be pretty much efficient to visit one of the ways versus. another according to that which you use it.alt text

I believe it's:

  Content.objects.filter(recommendation_set__user__publication_set__subscriber__pk=request.user.pk)/.distinct()/

or

  Recommendation.objects.filter(user__publication_set__subscriber__pk=request.user.pk)/.distinct()/

-- based on cases of which model you need to get. Distinct() may be required to avoid replicates.