1

I have some models like this:

class Container(models.Model):
    pass

class Parent(models.Model):
    container = models.ForeignKey(Container, related_name='items')
    pass

class Child(Parent):
    pass

class RedHeadedStepChild(Parent):
    pass

Is it possible to use select_subclasses() to prefetch fields in the container? I want to do something like this:

qs = Container.objects.all().prefetch_related('items')\
     .select_subclasses() # <---

So that the items related field of each Container is retrieved in each of its respective subclass types.

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164

1 Answers1

3

What about:

Container.objects.prefetch_related(
    Prefetch('items', Parent.objects.select_subclasses())
)
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
Simon Charette
  • 5,009
  • 1
  • 25
  • 33
  • This is the bee's knees. I've been working around this issue for months and now I have the solution. Thank you! – Ross Rogers Mar 12 '15 at 17:42
  • Thanks for the typo edit. I copy pasted `select_suclasses()` from your original question so you might want to correct it there too. Cheers. – Simon Charette Mar 12 '15 at 18:45