Reputation: 3471
I have two models as listed below:
class Project(models.Model):
name = models.TexField()
class Item(models.Model):
project = models.ForeignKey(Project,on_delete=models.CASCADE, related_name='project_item')
name = models.TextField()
available = models.FloatField()
So I need to fetch all Projects, however I want them such that, all those Projects which have Items that have available
greater than 0
, should come first.
I tried this:
list = Projects.objects.filter(**Some Fields**).order_by(available)
How can I sort this list?
Upvotes: 2
Views: 100
Reputation: 5482
You can follow relations in ordering clauses with double underscore like so:
list = Projects.objects.filter(**Some Fields**).order_by('-item__available')
This orders projects in descending order by related items' available field.
Upvotes: 3