Django SELECT statement, order by

Suppose I have 2 models.

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

I would like to select information from the first model, but ORDER as the 2nd model. How can i do this?

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) 
+4
source share
1 answer

I think you can do:

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

More generally, when models are related, you can select, organize, and filter fields by a "different" model using the relationshipName__fieldName alias of the model you select.

+7
source

All Articles