Django- getting a list of foreign key objects

Viewed 23599

Lets say I have the following models:

class ParentModel(models.Model):
    name = models.CharField()
    child = models.ForeignKey("ChildModel")

class ChildModel(models.Model):
    name = models.CharField()

Now, given some filter on ParentModels, I want to retrieve a list of all children models. I have tried:

children = ParentModel.objects.filter(name__startswith='A').values_list('child', flat=True)

However this returns a list of ChildModel ids, rather than the full objects. Is there a queryset function that will accomplish what I am trying to do or do I need to write an additional filter query using the returned ids? I.e.- instead of:

children => [51L, 53L, 54L]

I want:

children => [<ChildModel: ChildModel Object>, <ChildModel: ChildModel Object>, <ChildModel: ChildModel Object>]
3 Answers
Related