1

I use polymorphic for two models in django, one of them has a one to one relation to another model.

from polymorphic.models import PolymorphicModel

class Base(PolymorphicModel):
    name = models.CharField(max_length=25)

class Engine(models.Model):
    name = models.CharField(max_length=25)

class A(Base):
    type = models.CharField(max_length=25)

class B(Base):
    name = models.CharField(max_length=25)
    engine = models.OneToOneField(Engine)

class Host(Base):
    base_type = models.ForeignKey(Base)

Now, I want to use prefetch_related for class Host

host = Host.objects.all().prefetch_related('base_type__engine__name')

But, It's not valid for some objects from model B.

Is there any way to prefetch Engine object when I fetch A object?

MKM
  • 503
  • 1
  • 7
  • 16
  • 1
    Look tutorial: https://django-polymorphic.readthedocs.org/en/latest/advanced.html#filtering-for-classes-equivalent-to-python-s-isinstance – Marin Jan 04 '16 at 14:40
  • Thank you. I see it before, but there is not a solution for me in that! – MKM Jan 06 '16 at 15:57

0 Answers0