zymud
zymud

Reputation: 2249

Django. Order by filtered backward related field

I have such models:

class Parent(models.Model):
    name = models.CharField(max_length=31)


class Child(models.Model):
    name = models.CharField(max_length=31)
    value = models.FloatField()
    parent = models.ForeignKey(Parent, related_name='children')

I need to sort Parent instances by children values, but consider only child with defined name.

What is the best way to do this?

Upvotes: 0

Views: 48

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You can pretty much do it as you describe:

Parent.objects.filter(child__name='defined name').order_by('child__value')

Upvotes: 2

Related Questions