Joff
Joff

Reputation: 12177

trouble finding the right django queryset for this

I have two models like this:

class Foo():
    name = CharField
    frequency = IntegerField

class Bar():
    thing = ForeignKey(Foo)
    content = TextField

I want to get a queryset that will be Bar objects sorted by a range of Foo objects. It obviously doesn't work, but it illustrates what I need.

Foo.objects.order_by('-frequency')[0:10].bar_set.all()

Upvotes: 0

Views: 34

Answers (1)

sebb
sebb

Reputation: 1966

if I am getting right what would you like to achieve, try this:

objs = Bar.objects.filter(thing__pk__in=Foo.objects.all().order_by('-frequency').values_list('pk', flat=True)[:10])

Upvotes: 1

Related Questions