开发者

Django SELECT statement, Order by

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 = model开发者_StackOverflow中文版s.OneToOneField(Content, primary_key=True)
    counter =  models.IntegerField(default=0)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜