tags:

views:

98

answers:

1

Suppose I have 2 models.

The 2nd model has a one-to-one relationship with the first model.

I'd like to select information from the first model, but ORDER BY the 2nd model. How can I do that?

class Content(models.Model):
    link =  models.TextField(blank=True)
    title =  models.TextField(blank=True)
    is_channel =  models.BooleanField(default=0, db_index=True)

class Score(models.Model):
    content = models.OneToOneField(Content, primary_key=True)
    counter =  models.IntegerField(default=0)
+7  A: 

I think you can do:

Content.objects.filter(...).order_by('score__counter')

More generally, when models have a relationship, you can select, order, and filter by fields on the "other" model using the relationshipName__fieldName pseudo attribute of the model which you are selecting on.

dcrosta